]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/i915/selftests/mock_gem_device.c
drm/i915: Mock the GEM device for self-testing
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / i915 / selftests / mock_gem_device.c
CommitLineData
66d9cb5d
CW
1/*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25#include <linux/pm_runtime.h>
26
27#include "mock_gem_device.h"
28#include "mock_gem_object.h"
29
30static void mock_device_release(struct drm_device *dev)
31{
32 struct drm_i915_private *i915 = to_i915(dev);
33
34 i915_gem_drain_freed_objects(i915);
35
36 kmem_cache_destroy(i915->objects);
37
38 drm_dev_fini(&i915->drm);
39 put_device(&i915->drm.pdev->dev);
40}
41
42static struct drm_driver mock_driver = {
43 .name = "mock",
44 .driver_features = DRIVER_GEM,
45 .release = mock_device_release,
46
47 .gem_close_object = i915_gem_close_object,
48 .gem_free_object_unlocked = i915_gem_free_object,
49};
50
51static void release_dev(struct device *dev)
52{
53 struct pci_dev *pdev = to_pci_dev(dev);
54
55 kfree(pdev);
56}
57
58struct drm_i915_private *mock_gem_device(void)
59{
60 struct drm_i915_private *i915;
61 struct pci_dev *pdev;
62 int err;
63
64 pdev = kzalloc(sizeof(*pdev) + sizeof(*i915), GFP_KERNEL);
65 if (!pdev)
66 goto err;
67
68 device_initialize(&pdev->dev);
69 pdev->dev.release = release_dev;
70 dev_set_name(&pdev->dev, "mock");
71 dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
72
73 pm_runtime_dont_use_autosuspend(&pdev->dev);
74 pm_runtime_get_sync(&pdev->dev);
75
76 i915 = (struct drm_i915_private *)(pdev + 1);
77 pci_set_drvdata(pdev, i915);
78
79 err = drm_dev_init(&i915->drm, &mock_driver, &pdev->dev);
80 if (err) {
81 pr_err("Failed to initialise mock GEM device: err=%d\n", err);
82 goto put_device;
83 }
84 i915->drm.pdev = pdev;
85 i915->drm.dev_private = i915;
86
87 mkwrite_device_info(i915)->gen = -1;
88
89 spin_lock_init(&i915->mm.object_stat_lock);
90
91 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
92 init_llist_head(&i915->mm.free_list);
93
94 i915->objects = KMEM_CACHE(mock_object, SLAB_HWCACHE_ALIGN);
95 if (!i915->objects)
96 goto put_device;
97
98 return i915;
99
100put_device:
101 put_device(&pdev->dev);
102err:
103 return NULL;
104}