1 // SPDX-License-Identifier: GPL-2.0 AND MIT 2 /* 3 * Copyright © 2023 Intel Corporation 4 */ 5 6 #include <kunit/test.h> 7 #include <kunit/static_stub.h> 8 #include <kunit/visibility.h> 9 10 #include <drm/drm_drv.h> 11 #include <drm/drm_kunit_helpers.h> 12 13 #include "tests/xe_kunit_helpers.h" 14 #include "tests/xe_pci_test.h" 15 #include "xe_device_types.h" 16 17 /** 18 * xe_kunit_helper_alloc_xe_device - Allocate a &xe_device for a KUnit test. 19 * @test: the &kunit where this &xe_device will be used 20 * @dev: The parent device object 21 * 22 * This function allocates xe_device using drm_kunit_helper_alloc_device(). 23 * The xe_device allocation is managed by the test. 24 * 25 * @dev should be allocated using drm_kunit_helper_alloc_device(). 26 * 27 * This function uses KUNIT_ASSERT to detect any allocation failures. 28 * 29 * Return: A pointer to the new &xe_device. 30 */ 31 struct xe_device *xe_kunit_helper_alloc_xe_device(struct kunit *test, 32 struct device *dev) 33 { 34 struct xe_device *xe; 35 36 xe = drm_kunit_helper_alloc_drm_device(test, dev, 37 struct xe_device, 38 drm, DRIVER_GEM); 39 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, xe); 40 return xe; 41 } 42 EXPORT_SYMBOL_IF_KUNIT(xe_kunit_helper_alloc_xe_device); 43 44 static void kunit_action_restore_priv(void *priv) 45 { 46 struct kunit *test = kunit_get_current_test(); 47 48 test->priv = priv; 49 } 50 51 /** 52 * xe_kunit_helper_xe_device_test_init - Prepare a &xe_device for a KUnit test. 53 * @test: the &kunit where this fake &xe_device will be used 54 * 55 * This function allocates and initializes a fake &xe_device and stores its 56 * pointer as &kunit.priv to allow the test code to access it. 57 * 58 * This function can be directly used as custom implementation of 59 * &kunit_suite.init. 60 * 61 * It is possible to prepare specific variant of the fake &xe_device by passing 62 * in &kunit.priv pointer to the struct xe_pci_fake_data supplemented with 63 * desired parameters prior to calling this function. 64 * 65 * This function uses KUNIT_ASSERT to detect any failures. 66 * 67 * Return: Always 0. 68 */ 69 int xe_kunit_helper_xe_device_test_init(struct kunit *test) 70 { 71 struct xe_device *xe; 72 struct device *dev; 73 int err; 74 75 dev = drm_kunit_helper_alloc_device(test); 76 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); 77 78 xe = xe_kunit_helper_alloc_xe_device(test, dev); 79 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, xe); 80 81 err = xe_pci_fake_device_init(xe); 82 KUNIT_ASSERT_EQ(test, err, 0); 83 84 err = kunit_add_action_or_reset(test, kunit_action_restore_priv, test->priv); 85 KUNIT_ASSERT_EQ(test, err, 0); 86 87 test->priv = xe; 88 return 0; 89 } 90 EXPORT_SYMBOL_IF_KUNIT(xe_kunit_helper_xe_device_test_init); 91