1 /*
2 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
3 *
4 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
5 * All Rights Reserved.
6 *
7 * Author Rickard E. (Rik) Faith <faith@valinux.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/fcntl.h>
31 #include <sys/specdev.h>
32 #include <sys/vnode.h>
33
34 #include <machine/bus.h>
35
36 #ifdef __HAVE_ACPI
37 #include <dev/acpi/acpidev.h>
38 #include <dev/acpi/acpivar.h>
39 #include <dev/acpi/dsdt.h>
40 #endif
41
42 #include <linux/debugfs.h>
43 #include <linux/fs.h>
44 #include <linux/module.h>
45 #include <linux/moduleparam.h>
46 #include <linux/mount.h>
47 #include <linux/pseudo_fs.h>
48 #include <linux/slab.h>
49 #include <linux/srcu.h>
50 #include <linux/xarray.h>
51 #include <linux/suspend.h>
52
53 #include <drm/drm_accel.h>
54 #include <drm/drm_cache.h>
55 #include <drm/drm_client.h>
56 #include <drm/drm_color_mgmt.h>
57 #include <drm/drm_drv.h>
58 #include <drm/drm_file.h>
59 #include <drm/drm_managed.h>
60 #include <drm/drm_mode_object.h>
61 #include <drm/drm_panic.h>
62 #include <drm/drm_print.h>
63 #include <drm/drm_privacy_screen_machine.h>
64
65 #include <drm/drm_gem.h>
66
67 #include "drm_crtc_internal.h"
68 #include "drm_internal.h"
69
70 MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl");
71 MODULE_DESCRIPTION("DRM shared core routines");
72 MODULE_LICENSE("GPL and additional rights");
73
74 DEFINE_XARRAY_ALLOC(drm_minors_xa);
75
76 /*
77 * If the drm core fails to init for whatever reason,
78 * we should prevent any drivers from registering with it.
79 * It's best to check this at drm_dev_init(), as some drivers
80 * prefer to embed struct drm_device into their own device
81 * structure and call drm_dev_init() themselves.
82 */
83 static bool drm_core_init_complete;
84
85 static struct dentry *drm_debugfs_root;
86
87 #ifdef notyet
88 DEFINE_STATIC_SRCU(drm_unplug_srcu);
89 #endif
90
91 /*
92 * Some functions are only called once on init regardless of how many times
93 * drm attaches. In linux this is handled via module_init()/module_exit()
94 */
95 int drm_refcnt;
96
97 struct drm_softc {
98 struct device sc_dev;
99 struct drm_device *sc_drm;
100 int sc_allocated;
101 };
102
103 struct drm_attach_args {
104 struct drm_device *drm;
105 const struct drm_driver *driver;
106 char *busid;
107 bus_dma_tag_t dmat;
108 bus_space_tag_t bst;
109 size_t busid_len;
110 int is_agp;
111 struct pci_attach_args *pa;
112 int primary;
113 };
114
115 void drm_linux_init(void);
116 void drm_linux_exit(void);
117 int drm_linux_acpi_notify(struct aml_node *, int, void *);
118
119 int drm_dequeue_event(struct drm_device *, struct drm_file *, size_t,
120 struct drm_pending_event **);
121
122 int drmprint(void *, const char *);
123 int drmsubmatch(struct device *, void *, void *);
124 const struct pci_device_id *
125 drm_find_description(int, int, const struct pci_device_id *);
126
127 int drm_file_cmp(struct drm_file *, struct drm_file *);
128 SPLAY_PROTOTYPE(drm_file_tree, drm_file, link, drm_file_cmp);
129
130 #define DRMDEVCF_PRIMARY 0
131 #define drmdevcf_primary cf_loc[DRMDEVCF_PRIMARY] /* spec'd as primary? */
132 #define DRMDEVCF_PRIMARY_UNK -1
133
134 /*
135 * DRM Minors
136 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
137 * of them is represented by a drm_minor object. Depending on the capabilities
138 * of the device-driver, different interfaces are registered.
139 *
140 * Minors can be accessed via dev->$minor_name. This pointer is either
141 * NULL or a valid drm_minor pointer and stays valid as long as the device is
142 * valid. This means, DRM minors have the same life-time as the underlying
143 * device. However, this doesn't mean that the minor is active. Minors are
144 * registered and unregistered dynamically according to device-state.
145 */
146
drm_minor_get_xa(enum drm_minor_type type)147 static struct xarray *drm_minor_get_xa(enum drm_minor_type type)
148 {
149 if (type == DRM_MINOR_PRIMARY || type == DRM_MINOR_RENDER)
150 return &drm_minors_xa;
151 #if IS_ENABLED(CONFIG_DRM_ACCEL)
152 else if (type == DRM_MINOR_ACCEL)
153 return &accel_minors_xa;
154 #endif
155 else
156 return ERR_PTR(-EOPNOTSUPP);
157 }
158
drm_minor_get_slot(struct drm_device * dev,enum drm_minor_type type)159 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
160 enum drm_minor_type type)
161 {
162 switch (type) {
163 case DRM_MINOR_PRIMARY:
164 return &dev->primary;
165 case DRM_MINOR_RENDER:
166 return &dev->render;
167 case DRM_MINOR_ACCEL:
168 return &dev->accel;
169 default:
170 BUG();
171 }
172 }
173
drm_minor_alloc_release(struct drm_device * dev,void * data)174 static void drm_minor_alloc_release(struct drm_device *dev, void *data)
175 {
176 struct drm_minor *minor = data;
177
178 WARN_ON(dev != minor->dev);
179
180 #ifdef __linux__
181 put_device(minor->kdev);
182 #endif
183
184 xa_erase(drm_minor_get_xa(minor->type), minor->index);
185 }
186
187 /*
188 * DRM used to support 64 devices, for backwards compatibility we need to maintain the
189 * minor allocation scheme where minors 0-63 are primary nodes, 64-127 are control nodes,
190 * and 128-191 are render nodes.
191 * After reaching the limit, we're allocating minors dynamically - first-come, first-serve.
192 * Accel nodes are using a distinct major, so the minors are allocated in continuous 0-MAX
193 * range.
194 */
195 #define DRM_MINOR_LIMIT(t) ({ \
196 typeof(t) _t = (t); \
197 _t == DRM_MINOR_ACCEL ? XA_LIMIT(0, ACCEL_MAX_MINORS) : XA_LIMIT(64 * _t, 64 * _t + 63); \
198 })
199 #define DRM_EXTENDED_MINOR_LIMIT XA_LIMIT(192, (1 << MINORBITS) - 1)
200
drm_minor_alloc(struct drm_device * dev,enum drm_minor_type type)201 static int drm_minor_alloc(struct drm_device *dev, enum drm_minor_type type)
202 {
203 struct drm_minor *minor;
204 int r;
205
206 minor = drmm_kzalloc(dev, sizeof(*minor), GFP_KERNEL);
207 if (!minor)
208 return -ENOMEM;
209
210 minor->type = type;
211 minor->dev = dev;
212
213 r = xa_alloc(drm_minor_get_xa(type), &minor->index,
214 NULL, DRM_MINOR_LIMIT(type), GFP_KERNEL);
215 if (r == -EBUSY && (type == DRM_MINOR_PRIMARY || type == DRM_MINOR_RENDER))
216 r = xa_alloc(&drm_minors_xa, &minor->index,
217 NULL, DRM_EXTENDED_MINOR_LIMIT, GFP_KERNEL);
218 if (r < 0)
219 return r;
220
221 r = drmm_add_action_or_reset(dev, drm_minor_alloc_release, minor);
222 if (r)
223 return r;
224
225 #ifdef __linux__
226 minor->kdev = drm_sysfs_minor_alloc(minor);
227 if (IS_ERR(minor->kdev))
228 return PTR_ERR(minor->kdev);
229 #endif
230
231 *drm_minor_get_slot(dev, type) = minor;
232 return 0;
233 }
234
drm_minor_register(struct drm_device * dev,enum drm_minor_type type)235 static int drm_minor_register(struct drm_device *dev, enum drm_minor_type type)
236 {
237 struct drm_minor *minor;
238 void *entry;
239 int ret;
240
241 DRM_DEBUG("\n");
242
243 minor = *drm_minor_get_slot(dev, type);
244 if (!minor)
245 return 0;
246
247 #ifdef __linux__
248 if (minor->type != DRM_MINOR_ACCEL) {
249 ret = drm_debugfs_register(minor, minor->index,
250 drm_debugfs_root);
251 if (ret) {
252 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
253 goto err_debugfs;
254 }
255 }
256
257 ret = device_add(minor->kdev);
258 if (ret)
259 goto err_debugfs;
260 #else
261 drm_debugfs_root = NULL;
262 #endif
263
264 /* replace NULL with @minor so lookups will succeed from now on */
265 entry = xa_store(drm_minor_get_xa(type), minor->index, minor, GFP_KERNEL);
266 if (xa_is_err(entry)) {
267 ret = xa_err(entry);
268 goto err_debugfs;
269 }
270 WARN_ON(entry);
271
272 DRM_DEBUG("new minor registered %d\n", minor->index);
273 return 0;
274
275 err_debugfs:
276 drm_debugfs_unregister(minor);
277 return ret;
278 }
279
drm_minor_unregister(struct drm_device * dev,enum drm_minor_type type)280 static void drm_minor_unregister(struct drm_device *dev, enum drm_minor_type type)
281 {
282 struct drm_minor *minor;
283
284 minor = *drm_minor_get_slot(dev, type);
285 #ifdef __linux__
286 if (!minor || !device_is_registered(minor->kdev))
287 #else
288 if (!minor)
289 #endif
290 return;
291
292 /* replace @minor with NULL so lookups will fail from now on */
293 xa_store(drm_minor_get_xa(type), minor->index, NULL, GFP_KERNEL);
294
295 #ifdef __linux__
296 device_del(minor->kdev);
297 #endif
298 dev_set_drvdata(minor->kdev, NULL); /* safety belt */
299 drm_debugfs_unregister(minor);
300 }
301
302 /*
303 * Looks up the given minor-ID and returns the respective DRM-minor object. The
304 * refence-count of the underlying device is increased so you must release this
305 * object with drm_minor_release().
306 *
307 * As long as you hold this minor, it is guaranteed that the object and the
308 * minor->dev pointer will stay valid! However, the device may get unplugged and
309 * unregistered while you hold the minor.
310 */
drm_minor_acquire(struct xarray * minor_xa,unsigned int minor_id)311 struct drm_minor *drm_minor_acquire(struct xarray *minor_xa, unsigned int minor_id)
312 {
313 struct drm_minor *minor;
314
315 xa_lock(minor_xa);
316 minor = xa_load(minor_xa, minor_id);
317 if (minor)
318 drm_dev_get(minor->dev);
319 xa_unlock(minor_xa);
320
321 if (!minor) {
322 return ERR_PTR(-ENODEV);
323 } else if (drm_dev_is_unplugged(minor->dev)) {
324 drm_dev_put(minor->dev);
325 return ERR_PTR(-ENODEV);
326 }
327
328 return minor;
329 }
330
drm_minor_release(struct drm_minor * minor)331 void drm_minor_release(struct drm_minor *minor)
332 {
333 drm_dev_put(minor->dev);
334 }
335
336 /**
337 * DOC: driver instance overview
338 *
339 * A device instance for a drm driver is represented by &struct drm_device. This
340 * is allocated and initialized with devm_drm_dev_alloc(), usually from
341 * bus-specific ->probe() callbacks implemented by the driver. The driver then
342 * needs to initialize all the various subsystems for the drm device like memory
343 * management, vblank handling, modesetting support and initial output
344 * configuration plus obviously initialize all the corresponding hardware bits.
345 * Finally when everything is up and running and ready for userspace the device
346 * instance can be published using drm_dev_register().
347 *
348 * There is also deprecated support for initializing device instances using
349 * bus-specific helpers and the &drm_driver.load callback. But due to
350 * backwards-compatibility needs the device instance have to be published too
351 * early, which requires unpretty global locking to make safe and is therefore
352 * only support for existing drivers not yet converted to the new scheme.
353 *
354 * When cleaning up a device instance everything needs to be done in reverse:
355 * First unpublish the device instance with drm_dev_unregister(). Then clean up
356 * any other resources allocated at device initialization and drop the driver's
357 * reference to &drm_device using drm_dev_put().
358 *
359 * Note that any allocation or resource which is visible to userspace must be
360 * released only when the final drm_dev_put() is called, and not when the
361 * driver is unbound from the underlying physical struct &device. Best to use
362 * &drm_device managed resources with drmm_add_action(), drmm_kmalloc() and
363 * related functions.
364 *
365 * devres managed resources like devm_kmalloc() can only be used for resources
366 * directly related to the underlying hardware device, and only used in code
367 * paths fully protected by drm_dev_enter() and drm_dev_exit().
368 *
369 * Display driver example
370 * ~~~~~~~~~~~~~~~~~~~~~~
371 *
372 * The following example shows a typical structure of a DRM display driver.
373 * The example focus on the probe() function and the other functions that is
374 * almost always present and serves as a demonstration of devm_drm_dev_alloc().
375 *
376 * .. code-block:: c
377 *
378 * struct driver_device {
379 * struct drm_device drm;
380 * void *userspace_facing;
381 * struct clk *pclk;
382 * };
383 *
384 * static const struct drm_driver driver_drm_driver = {
385 * [...]
386 * };
387 *
388 * static int driver_probe(struct platform_device *pdev)
389 * {
390 * struct driver_device *priv;
391 * struct drm_device *drm;
392 * int ret;
393 *
394 * priv = devm_drm_dev_alloc(&pdev->dev, &driver_drm_driver,
395 * struct driver_device, drm);
396 * if (IS_ERR(priv))
397 * return PTR_ERR(priv);
398 * drm = &priv->drm;
399 *
400 * ret = drmm_mode_config_init(drm);
401 * if (ret)
402 * return ret;
403 *
404 * priv->userspace_facing = drmm_kzalloc(..., GFP_KERNEL);
405 * if (!priv->userspace_facing)
406 * return -ENOMEM;
407 *
408 * priv->pclk = devm_clk_get(dev, "PCLK");
409 * if (IS_ERR(priv->pclk))
410 * return PTR_ERR(priv->pclk);
411 *
412 * // Further setup, display pipeline etc
413 *
414 * platform_set_drvdata(pdev, drm);
415 *
416 * drm_mode_config_reset(drm);
417 *
418 * ret = drm_dev_register(drm);
419 * if (ret)
420 * return ret;
421 *
422 * drm_fbdev_{...}_setup(drm, 32);
423 *
424 * return 0;
425 * }
426 *
427 * // This function is called before the devm_ resources are released
428 * static int driver_remove(struct platform_device *pdev)
429 * {
430 * struct drm_device *drm = platform_get_drvdata(pdev);
431 *
432 * drm_dev_unregister(drm);
433 * drm_atomic_helper_shutdown(drm)
434 *
435 * return 0;
436 * }
437 *
438 * // This function is called on kernel restart and shutdown
439 * static void driver_shutdown(struct platform_device *pdev)
440 * {
441 * drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
442 * }
443 *
444 * static int __maybe_unused driver_pm_suspend(struct device *dev)
445 * {
446 * return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
447 * }
448 *
449 * static int __maybe_unused driver_pm_resume(struct device *dev)
450 * {
451 * drm_mode_config_helper_resume(dev_get_drvdata(dev));
452 *
453 * return 0;
454 * }
455 *
456 * static const struct dev_pm_ops driver_pm_ops = {
457 * SET_SYSTEM_SLEEP_PM_OPS(driver_pm_suspend, driver_pm_resume)
458 * };
459 *
460 * static struct platform_driver driver_driver = {
461 * .driver = {
462 * [...]
463 * .pm = &driver_pm_ops,
464 * },
465 * .probe = driver_probe,
466 * .remove = driver_remove,
467 * .shutdown = driver_shutdown,
468 * };
469 * module_platform_driver(driver_driver);
470 *
471 * Drivers that want to support device unplugging (USB, DT overlay unload) should
472 * use drm_dev_unplug() instead of drm_dev_unregister(). The driver must protect
473 * regions that is accessing device resources to prevent use after they're
474 * released. This is done using drm_dev_enter() and drm_dev_exit(). There is one
475 * shortcoming however, drm_dev_unplug() marks the drm_device as unplugged before
476 * drm_atomic_helper_shutdown() is called. This means that if the disable code
477 * paths are protected, they will not run on regular driver module unload,
478 * possibly leaving the hardware enabled.
479 */
480
481 /**
482 * drm_put_dev - Unregister and release a DRM device
483 * @dev: DRM device
484 *
485 * Called at module unload time or when a PCI device is unplugged.
486 *
487 * Cleans up all DRM device, calling drm_lastclose().
488 *
489 * Note: Use of this function is deprecated. It will eventually go away
490 * completely. Please use drm_dev_unregister() and drm_dev_put() explicitly
491 * instead to make sure that the device isn't userspace accessible any more
492 * while teardown is in progress, ensuring that userspace can't access an
493 * inconsistent state.
494 */
drm_put_dev(struct drm_device * dev)495 void drm_put_dev(struct drm_device *dev)
496 {
497 DRM_DEBUG("\n");
498
499 if (!dev) {
500 DRM_ERROR("cleanup called no dev\n");
501 return;
502 }
503
504 drm_dev_unregister(dev);
505 drm_dev_put(dev);
506 }
507 EXPORT_SYMBOL(drm_put_dev);
508
509 /**
510 * drm_dev_enter - Enter device critical section
511 * @dev: DRM device
512 * @idx: Pointer to index that will be passed to the matching drm_dev_exit()
513 *
514 * This function marks and protects the beginning of a section that should not
515 * be entered after the device has been unplugged. The section end is marked
516 * with drm_dev_exit(). Calls to this function can be nested.
517 *
518 * Returns:
519 * True if it is OK to enter the section, false otherwise.
520 */
drm_dev_enter(struct drm_device * dev,int * idx)521 bool drm_dev_enter(struct drm_device *dev, int *idx)
522 {
523 #ifdef notyet
524 *idx = srcu_read_lock(&drm_unplug_srcu);
525
526 if (dev->unplugged) {
527 srcu_read_unlock(&drm_unplug_srcu, *idx);
528 return false;
529 }
530 #endif
531
532 return true;
533 }
534 EXPORT_SYMBOL(drm_dev_enter);
535
536 /**
537 * drm_dev_exit - Exit device critical section
538 * @idx: index returned from drm_dev_enter()
539 *
540 * This function marks the end of a section that should not be entered after
541 * the device has been unplugged.
542 */
drm_dev_exit(int idx)543 void drm_dev_exit(int idx)
544 {
545 #ifdef notyet
546 srcu_read_unlock(&drm_unplug_srcu, idx);
547 #endif
548 }
549 EXPORT_SYMBOL(drm_dev_exit);
550
551 /**
552 * drm_dev_unplug - unplug a DRM device
553 * @dev: DRM device
554 *
555 * This unplugs a hotpluggable DRM device, which makes it inaccessible to
556 * userspace operations. Entry-points can use drm_dev_enter() and
557 * drm_dev_exit() to protect device resources in a race free manner. This
558 * essentially unregisters the device like drm_dev_unregister(), but can be
559 * called while there are still open users of @dev.
560 */
drm_dev_unplug(struct drm_device * dev)561 void drm_dev_unplug(struct drm_device *dev)
562 {
563 STUB();
564 #ifdef notyet
565 /*
566 * After synchronizing any critical read section is guaranteed to see
567 * the new value of ->unplugged, and any critical section which might
568 * still have seen the old value of ->unplugged is guaranteed to have
569 * finished.
570 */
571 dev->unplugged = true;
572 synchronize_srcu(&drm_unplug_srcu);
573
574 drm_dev_unregister(dev);
575
576 /* Clear all CPU mappings pointing to this device */
577 unmap_mapping_range(dev->anon_inode->i_mapping, 0, 0, 1);
578 #endif
579 }
580 EXPORT_SYMBOL(drm_dev_unplug);
581
582 #ifdef __linux__
583 /*
584 * DRM internal mount
585 * We want to be able to allocate our own "struct address_space" to control
586 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
587 * stand-alone address_space objects, so we need an underlying inode. As there
588 * is no way to allocate an independent inode easily, we need a fake internal
589 * VFS mount-point.
590 *
591 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
592 * frees it again. You are allowed to use iget() and iput() to get references to
593 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
594 * drm_fs_inode_free() call (which does not have to be the last iput()).
595 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
596 * between multiple inode-users. You could, technically, call
597 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
598 * iput(), but this way you'd end up with a new vfsmount for each inode.
599 */
600
601 static int drm_fs_cnt;
602 static struct vfsmount *drm_fs_mnt;
603
drm_fs_init_fs_context(struct fs_context * fc)604 static int drm_fs_init_fs_context(struct fs_context *fc)
605 {
606 return init_pseudo(fc, 0x010203ff) ? 0 : -ENOMEM;
607 }
608
609 static struct file_system_type drm_fs_type = {
610 .name = "drm",
611 .owner = THIS_MODULE,
612 .init_fs_context = drm_fs_init_fs_context,
613 .kill_sb = kill_anon_super,
614 };
615
drm_fs_inode_new(void)616 static struct inode *drm_fs_inode_new(void)
617 {
618 struct inode *inode;
619 int r;
620
621 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
622 if (r < 0) {
623 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
624 return ERR_PTR(r);
625 }
626
627 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
628 if (IS_ERR(inode))
629 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
630
631 return inode;
632 }
633
drm_fs_inode_free(struct inode * inode)634 static void drm_fs_inode_free(struct inode *inode)
635 {
636 if (inode) {
637 iput(inode);
638 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
639 }
640 }
641
642 #endif /* __linux__ */
643
644 /**
645 * DOC: component helper usage recommendations
646 *
647 * DRM drivers that drive hardware where a logical device consists of a pile of
648 * independent hardware blocks are recommended to use the :ref:`component helper
649 * library<component>`. For consistency and better options for code reuse the
650 * following guidelines apply:
651 *
652 * - The entire device initialization procedure should be run from the
653 * &component_master_ops.master_bind callback, starting with
654 * devm_drm_dev_alloc(), then binding all components with
655 * component_bind_all() and finishing with drm_dev_register().
656 *
657 * - The opaque pointer passed to all components through component_bind_all()
658 * should point at &struct drm_device of the device instance, not some driver
659 * specific private structure.
660 *
661 * - The component helper fills the niche where further standardization of
662 * interfaces is not practical. When there already is, or will be, a
663 * standardized interface like &drm_bridge or &drm_panel, providing its own
664 * functions to find such components at driver load time, like
665 * drm_of_find_panel_or_bridge(), then the component helper should not be
666 * used.
667 */
668
drm_dev_init_release(struct drm_device * dev,void * res)669 static void drm_dev_init_release(struct drm_device *dev, void *res)
670 {
671 #ifdef __linux__
672 drm_fs_inode_free(dev->anon_inode);
673
674 put_device(dev->dev);
675 #endif
676 /* Prevent use-after-free in drm_managed_release when debugging is
677 * enabled. Slightly awkward, but can't really be helped. */
678 dev->dev = NULL;
679 mutex_destroy(&dev->master_mutex);
680 mutex_destroy(&dev->clientlist_mutex);
681 mutex_destroy(&dev->filelist_mutex);
682 mutex_destroy(&dev->struct_mutex);
683 }
684
685 #ifdef notyet
686
drm_dev_init(struct drm_device * dev,const struct drm_driver * driver,struct device * parent)687 static int drm_dev_init(struct drm_device *dev,
688 const struct drm_driver *driver,
689 struct device *parent)
690 {
691 struct inode *inode;
692 int ret;
693
694 if (!drm_core_init_complete) {
695 DRM_ERROR("DRM core is not initialized\n");
696 return -ENODEV;
697 }
698
699 if (WARN_ON(!parent))
700 return -EINVAL;
701
702 kref_init(&dev->ref);
703 dev->dev = get_device(parent);
704 dev->driver = driver;
705
706 INIT_LIST_HEAD(&dev->managed.resources);
707 spin_lock_init(&dev->managed.lock);
708
709 /* no per-device feature limits by default */
710 dev->driver_features = ~0u;
711
712 if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL) &&
713 (drm_core_check_feature(dev, DRIVER_RENDER) ||
714 drm_core_check_feature(dev, DRIVER_MODESET))) {
715 DRM_ERROR("DRM driver can't be both a compute acceleration and graphics driver\n");
716 return -EINVAL;
717 }
718
719 INIT_LIST_HEAD(&dev->filelist);
720 INIT_LIST_HEAD(&dev->filelist_internal);
721 INIT_LIST_HEAD(&dev->clientlist);
722 INIT_LIST_HEAD(&dev->vblank_event_list);
723
724 spin_lock_init(&dev->event_lock);
725 mutex_init(&dev->struct_mutex);
726 mutex_init(&dev->filelist_mutex);
727 mutex_init(&dev->clientlist_mutex);
728 mutex_init(&dev->master_mutex);
729 raw_spin_lock_init(&dev->mode_config.panic_lock);
730
731 ret = drmm_add_action_or_reset(dev, drm_dev_init_release, NULL);
732 if (ret)
733 return ret;
734
735 inode = drm_fs_inode_new();
736 if (IS_ERR(inode)) {
737 ret = PTR_ERR(inode);
738 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
739 goto err;
740 }
741
742 dev->anon_inode = inode;
743
744 if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL)) {
745 ret = drm_minor_alloc(dev, DRM_MINOR_ACCEL);
746 if (ret)
747 goto err;
748 } else {
749 if (drm_core_check_feature(dev, DRIVER_RENDER)) {
750 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
751 if (ret)
752 goto err;
753 }
754
755 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY);
756 if (ret)
757 goto err;
758 }
759
760 if (drm_core_check_feature(dev, DRIVER_GEM)) {
761 ret = drm_gem_init(dev);
762 if (ret) {
763 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
764 goto err;
765 }
766 }
767
768 dev->unique = drmm_kstrdup(dev, dev_name(parent), GFP_KERNEL);
769 if (!dev->unique) {
770 ret = -ENOMEM;
771 goto err;
772 }
773
774 if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
775 accel_debugfs_init(dev);
776 else
777 drm_debugfs_dev_init(dev, drm_debugfs_root);
778
779 return 0;
780
781 err:
782 drm_managed_release(dev);
783
784 return ret;
785 }
786
devm_drm_dev_init_release(void * data)787 static void devm_drm_dev_init_release(void *data)
788 {
789 drm_dev_put(data);
790 }
791
devm_drm_dev_init(struct device * parent,struct drm_device * dev,const struct drm_driver * driver)792 static int devm_drm_dev_init(struct device *parent,
793 struct drm_device *dev,
794 const struct drm_driver *driver)
795 {
796 int ret;
797
798 ret = drm_dev_init(dev, driver, parent);
799 if (ret)
800 return ret;
801
802 return devm_add_action_or_reset(parent,
803 devm_drm_dev_init_release, dev);
804 }
805
806 #endif
807
__devm_drm_dev_alloc(struct device * parent,const struct drm_driver * driver,size_t size,size_t offset)808 void *__devm_drm_dev_alloc(struct device *parent,
809 const struct drm_driver *driver,
810 size_t size, size_t offset)
811 {
812 void *container;
813 struct drm_device *drm;
814 #ifdef notyet
815 int ret;
816 #endif
817
818 container = kzalloc(size, GFP_KERNEL);
819 if (!container)
820 return ERR_PTR(-ENOMEM);
821
822 drm = container + offset;
823 #ifdef notyet
824 ret = devm_drm_dev_init(parent, drm, driver);
825 if (ret) {
826 kfree(container);
827 return ERR_PTR(ret);
828 }
829 drmm_add_final_kfree(drm, container);
830 #endif
831
832 return container;
833 }
834 EXPORT_SYMBOL(__devm_drm_dev_alloc);
835
836 #ifdef notyet
837
838 /**
839 * drm_dev_alloc - Allocate new DRM device
840 * @driver: DRM driver to allocate device for
841 * @parent: Parent device object
842 *
843 * This is the deprecated version of devm_drm_dev_alloc(), which does not support
844 * subclassing through embedding the struct &drm_device in a driver private
845 * structure, and which does not support automatic cleanup through devres.
846 *
847 * RETURNS:
848 * Pointer to new DRM device, or ERR_PTR on failure.
849 */
drm_dev_alloc(const struct drm_driver * driver,struct device * parent)850 struct drm_device *drm_dev_alloc(const struct drm_driver *driver,
851 struct device *parent)
852 {
853 struct drm_device *dev;
854 int ret;
855
856 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
857 if (!dev)
858 return ERR_PTR(-ENOMEM);
859
860 ret = drm_dev_init(dev, driver, parent);
861 if (ret) {
862 kfree(dev);
863 return ERR_PTR(ret);
864 }
865
866 drmm_add_final_kfree(dev, dev);
867
868 return dev;
869 }
870 EXPORT_SYMBOL(drm_dev_alloc);
871
872 #endif
873
drm_dev_release(struct kref * ref)874 static void drm_dev_release(struct kref *ref)
875 {
876 struct drm_device *dev = container_of(ref, struct drm_device, ref);
877
878 /* Just in case register/unregister was never called */
879 drm_debugfs_dev_fini(dev);
880
881 if (dev->driver->release)
882 dev->driver->release(dev);
883
884 drm_managed_release(dev);
885
886 kfree(dev->managed.final_kfree);
887 }
888
889 /**
890 * drm_dev_get - Take reference of a DRM device
891 * @dev: device to take reference of or NULL
892 *
893 * This increases the ref-count of @dev by one. You *must* already own a
894 * reference when calling this. Use drm_dev_put() to drop this reference
895 * again.
896 *
897 * This function never fails. However, this function does not provide *any*
898 * guarantee whether the device is alive or running. It only provides a
899 * reference to the object and the memory associated with it.
900 */
drm_dev_get(struct drm_device * dev)901 void drm_dev_get(struct drm_device *dev)
902 {
903 if (dev)
904 kref_get(&dev->ref);
905 }
906 EXPORT_SYMBOL(drm_dev_get);
907
908 /**
909 * drm_dev_put - Drop reference of a DRM device
910 * @dev: device to drop reference of or NULL
911 *
912 * This decreases the ref-count of @dev by one. The device is destroyed if the
913 * ref-count drops to zero.
914 */
drm_dev_put(struct drm_device * dev)915 void drm_dev_put(struct drm_device *dev)
916 {
917 if (dev)
918 kref_put(&dev->ref, drm_dev_release);
919 }
920 EXPORT_SYMBOL(drm_dev_put);
921
create_compat_control_link(struct drm_device * dev)922 static int create_compat_control_link(struct drm_device *dev)
923 {
924 struct drm_minor *minor;
925 char *name;
926 int ret;
927
928 if (!drm_core_check_feature(dev, DRIVER_MODESET))
929 return 0;
930
931 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
932 if (!minor)
933 return 0;
934
935 /*
936 * Some existing userspace out there uses the existing of the controlD*
937 * sysfs files to figure out whether it's a modeset driver. It only does
938 * readdir, hence a symlink is sufficient (and the least confusing
939 * option). Otherwise controlD* is entirely unused.
940 *
941 * Old controlD chardev have been allocated in the range
942 * 64-127.
943 */
944 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
945 if (!name)
946 return -ENOMEM;
947
948 ret = sysfs_create_link(minor->kdev->kobj.parent,
949 &minor->kdev->kobj,
950 name);
951
952 kfree(name);
953
954 return ret;
955 }
956
remove_compat_control_link(struct drm_device * dev)957 static void remove_compat_control_link(struct drm_device *dev)
958 {
959 struct drm_minor *minor;
960 char *name;
961
962 if (!drm_core_check_feature(dev, DRIVER_MODESET))
963 return;
964
965 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
966 if (!minor)
967 return;
968
969 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
970 if (!name)
971 return;
972
973 sysfs_remove_link(minor->kdev->kobj.parent, name);
974
975 kfree(name);
976 }
977
978 /**
979 * drm_dev_register - Register DRM device
980 * @dev: Device to register
981 * @flags: Flags passed to the driver's .load() function
982 *
983 * Register the DRM device @dev with the system, advertise device to user-space
984 * and start normal device operation. @dev must be initialized via drm_dev_init()
985 * previously.
986 *
987 * Never call this twice on any device!
988 *
989 * NOTE: To ensure backward compatibility with existing drivers method this
990 * function calls the &drm_driver.load method after registering the device
991 * nodes, creating race conditions. Usage of the &drm_driver.load methods is
992 * therefore deprecated, drivers must perform all initialization before calling
993 * drm_dev_register().
994 *
995 * RETURNS:
996 * 0 on success, negative error code on failure.
997 */
drm_dev_register(struct drm_device * dev,unsigned long flags)998 int drm_dev_register(struct drm_device *dev, unsigned long flags)
999 {
1000 const struct drm_driver *driver = dev->driver;
1001 int ret;
1002
1003 if (!driver->load)
1004 drm_mode_config_validate(dev);
1005
1006 WARN_ON(!dev->managed.final_kfree);
1007
1008 if (drm_dev_needs_global_mutex(dev))
1009 mutex_lock(&drm_global_mutex);
1010
1011 if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
1012 accel_debugfs_register(dev);
1013 else
1014 drm_debugfs_dev_register(dev);
1015
1016 ret = drm_minor_register(dev, DRM_MINOR_RENDER);
1017 if (ret)
1018 goto err_minors;
1019
1020 ret = drm_minor_register(dev, DRM_MINOR_PRIMARY);
1021 if (ret)
1022 goto err_minors;
1023
1024 ret = drm_minor_register(dev, DRM_MINOR_ACCEL);
1025 if (ret)
1026 goto err_minors;
1027
1028 ret = create_compat_control_link(dev);
1029 if (ret)
1030 goto err_minors;
1031
1032 dev->registered = true;
1033
1034 if (driver->load) {
1035 ret = driver->load(dev, flags);
1036 if (ret)
1037 goto err_minors;
1038 }
1039
1040 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1041 ret = drm_modeset_register_all(dev);
1042 if (ret)
1043 goto err_unload;
1044 }
1045 drm_panic_register(dev);
1046
1047 DRM_INFO("Initialized %s %d.%d.%d for %s on minor %d\n",
1048 driver->name, driver->major, driver->minor,
1049 driver->patchlevel,
1050 dev->dev ? dev_name(dev->dev) : "virtual device",
1051 dev->primary ? dev->primary->index : dev->accel->index);
1052
1053 goto out_unlock;
1054
1055 err_unload:
1056 if (dev->driver->unload)
1057 dev->driver->unload(dev);
1058 err_minors:
1059 remove_compat_control_link(dev);
1060 drm_minor_unregister(dev, DRM_MINOR_ACCEL);
1061 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
1062 drm_minor_unregister(dev, DRM_MINOR_RENDER);
1063 out_unlock:
1064 if (drm_dev_needs_global_mutex(dev))
1065 mutex_unlock(&drm_global_mutex);
1066 return ret;
1067 }
1068 EXPORT_SYMBOL(drm_dev_register);
1069
1070 /**
1071 * drm_dev_unregister - Unregister DRM device
1072 * @dev: Device to unregister
1073 *
1074 * Unregister the DRM device from the system. This does the reverse of
1075 * drm_dev_register() but does not deallocate the device. The caller must call
1076 * drm_dev_put() to drop their final reference, unless it is managed with devres
1077 * (as devices allocated with devm_drm_dev_alloc() are), in which case there is
1078 * already an unwind action registered.
1079 *
1080 * A special form of unregistering for hotpluggable devices is drm_dev_unplug(),
1081 * which can be called while there are still open users of @dev.
1082 *
1083 * This should be called first in the device teardown code to make sure
1084 * userspace can't access the device instance any more.
1085 */
drm_dev_unregister(struct drm_device * dev)1086 void drm_dev_unregister(struct drm_device *dev)
1087 {
1088 dev->registered = false;
1089
1090 drm_panic_unregister(dev);
1091
1092 drm_client_dev_unregister(dev);
1093
1094 if (drm_core_check_feature(dev, DRIVER_MODESET))
1095 drm_modeset_unregister_all(dev);
1096
1097 if (dev->driver->unload)
1098 dev->driver->unload(dev);
1099
1100 remove_compat_control_link(dev);
1101 drm_minor_unregister(dev, DRM_MINOR_ACCEL);
1102 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
1103 drm_minor_unregister(dev, DRM_MINOR_RENDER);
1104 drm_debugfs_dev_fini(dev);
1105 }
1106 EXPORT_SYMBOL(drm_dev_unregister);
1107
1108 /*
1109 * DRM Core
1110 * The DRM core module initializes all global DRM objects and makes them
1111 * available to drivers. Once setup, drivers can probe their respective
1112 * devices.
1113 * Currently, core management includes:
1114 * - The "DRM-Global" key/value database
1115 * - Global ID management for connectors
1116 * - DRM major number allocation
1117 * - DRM minor management
1118 * - DRM sysfs class
1119 * - DRM debugfs root
1120 *
1121 * Furthermore, the DRM core provides dynamic char-dev lookups. For each
1122 * interface registered on a DRM device, you can request minor numbers from DRM
1123 * core. DRM core takes care of major-number management and char-dev
1124 * registration. A stub ->open() callback forwards any open() requests to the
1125 * registered minor.
1126 */
1127
1128 #ifdef __linux__
drm_stub_open(struct inode * inode,struct file * filp)1129 static int drm_stub_open(struct inode *inode, struct file *filp)
1130 {
1131 const struct file_operations *new_fops;
1132 struct drm_minor *minor;
1133 int err;
1134
1135 DRM_DEBUG("\n");
1136
1137 minor = drm_minor_acquire(&drm_minors_xa, iminor(inode));
1138 if (IS_ERR(minor))
1139 return PTR_ERR(minor);
1140
1141 new_fops = fops_get(minor->dev->driver->fops);
1142 if (!new_fops) {
1143 err = -ENODEV;
1144 goto out;
1145 }
1146
1147 replace_fops(filp, new_fops);
1148 if (filp->f_op->open)
1149 err = filp->f_op->open(inode, filp);
1150 else
1151 err = 0;
1152
1153 out:
1154 drm_minor_release(minor);
1155
1156 return err;
1157 }
1158
1159 static const struct file_operations drm_stub_fops = {
1160 .owner = THIS_MODULE,
1161 .open = drm_stub_open,
1162 .llseek = noop_llseek,
1163 };
1164 #endif /* __linux__ */
1165
drm_core_exit(void)1166 static void drm_core_exit(void)
1167 {
1168 drm_privacy_screen_lookup_exit();
1169 drm_panic_exit();
1170 accel_core_exit();
1171 #ifdef __linux__
1172 unregister_chrdev(DRM_MAJOR, "drm");
1173 debugfs_remove(drm_debugfs_root);
1174 drm_sysfs_destroy();
1175 #endif
1176 WARN_ON(!xa_empty(&drm_minors_xa));
1177 drm_connector_ida_destroy();
1178 }
1179
drm_core_init(void)1180 static int __init drm_core_init(void)
1181 {
1182 #ifdef __linux__
1183 int ret;
1184 #endif
1185
1186 drm_connector_ida_init();
1187 drm_memcpy_init_early();
1188
1189 #ifdef __linux__
1190 ret = drm_sysfs_init();
1191 if (ret < 0) {
1192 DRM_ERROR("Cannot create DRM class: %d\n", ret);
1193 goto error;
1194 }
1195
1196 drm_debugfs_root = debugfs_create_dir("dri", NULL);
1197
1198 ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
1199 if (ret < 0)
1200 goto error;
1201
1202 ret = accel_core_init();
1203 if (ret < 0)
1204 goto error;
1205 #endif
1206
1207 drm_panic_init();
1208
1209 drm_privacy_screen_lookup_init();
1210
1211 drm_core_init_complete = true;
1212
1213 DRM_DEBUG("Initialized\n");
1214 return 0;
1215 #ifdef __linux__
1216 error:
1217 drm_core_exit();
1218 return ret;
1219 #endif
1220 }
1221
1222 #ifdef __linux__
1223 module_init(drm_core_init);
1224 module_exit(drm_core_exit);
1225 #endif
1226
1227 void drm_lastclose(struct drm_device *dev);
1228
1229 void
drm_attach_platform(struct drm_driver * driver,bus_space_tag_t iot,bus_dma_tag_t dmat,struct device * dev,struct drm_device * drm)1230 drm_attach_platform(struct drm_driver *driver, bus_space_tag_t iot,
1231 bus_dma_tag_t dmat, struct device *dev, struct drm_device *drm)
1232 {
1233 struct drm_attach_args arg;
1234
1235 memset(&arg, 0, sizeof(arg));
1236 arg.driver = driver;
1237 arg.bst = iot;
1238 arg.dmat = dmat;
1239 arg.drm = drm;
1240
1241 arg.busid = dev->dv_xname;
1242 arg.busid_len = strlen(dev->dv_xname) + 1;
1243 config_found_sm(dev, &arg, drmprint, drmsubmatch);
1244 }
1245
1246 struct drm_device *
drm_attach_pci(const struct drm_driver * driver,struct pci_attach_args * pa,int is_agp,int primary,struct device * dev,struct drm_device * drm)1247 drm_attach_pci(const struct drm_driver *driver, struct pci_attach_args *pa,
1248 int is_agp, int primary, struct device *dev, struct drm_device *drm)
1249 {
1250 struct drm_attach_args arg;
1251 struct drm_softc *sc;
1252
1253 arg.drm = drm;
1254 arg.driver = driver;
1255 arg.dmat = pa->pa_dmat;
1256 arg.bst = pa->pa_memt;
1257 arg.is_agp = is_agp;
1258 arg.primary = primary;
1259 arg.pa = pa;
1260
1261 arg.busid_len = 20;
1262 arg.busid = malloc(arg.busid_len + 1, M_DRM, M_NOWAIT);
1263 if (arg.busid == NULL) {
1264 printf("%s: no memory for drm\n", dev->dv_xname);
1265 return (NULL);
1266 }
1267 snprintf(arg.busid, arg.busid_len, "pci:%04x:%02x:%02x.%1x",
1268 pa->pa_domain, pa->pa_bus, pa->pa_device, pa->pa_function);
1269
1270 sc = (struct drm_softc *)config_found_sm(dev, &arg, drmprint, drmsubmatch);
1271 if (sc == NULL)
1272 return NULL;
1273
1274 return sc->sc_drm;
1275 }
1276
1277 int
drmprint(void * aux,const char * pnp)1278 drmprint(void *aux, const char *pnp)
1279 {
1280 if (pnp != NULL)
1281 printf("drm at %s", pnp);
1282 return (UNCONF);
1283 }
1284
1285 int
drmsubmatch(struct device * parent,void * match,void * aux)1286 drmsubmatch(struct device *parent, void *match, void *aux)
1287 {
1288 extern struct cfdriver drm_cd;
1289 struct cfdata *cf = match;
1290
1291 /* only allow drm to attach */
1292 if (cf->cf_driver == &drm_cd)
1293 return ((*cf->cf_attach->ca_match)(parent, match, aux));
1294 return (0);
1295 }
1296
1297 int
drm_pciprobe(struct pci_attach_args * pa,const struct pci_device_id * idlist)1298 drm_pciprobe(struct pci_attach_args *pa, const struct pci_device_id *idlist)
1299 {
1300 const struct pci_device_id *id_entry;
1301
1302 id_entry = drm_find_description(PCI_VENDOR(pa->pa_id),
1303 PCI_PRODUCT(pa->pa_id), idlist);
1304 if (id_entry != NULL)
1305 return 1;
1306
1307 return 0;
1308 }
1309
1310 int
drm_probe(struct device * parent,void * match,void * aux)1311 drm_probe(struct device *parent, void *match, void *aux)
1312 {
1313 struct cfdata *cf = match;
1314 struct drm_attach_args *da = aux;
1315
1316 if (cf->drmdevcf_primary != DRMDEVCF_PRIMARY_UNK) {
1317 /*
1318 * If primary-ness of device specified, either match
1319 * exactly (at high priority), or fail.
1320 */
1321 if (cf->drmdevcf_primary != 0 && da->primary != 0)
1322 return (10);
1323 else
1324 return (0);
1325 }
1326
1327 /* If primary-ness unspecified, it wins. */
1328 return (1);
1329 }
1330
1331 int drm_buddy_module_init(void);
1332 void drm_buddy_module_exit(void);
1333
1334 void
drm_attach(struct device * parent,struct device * self,void * aux)1335 drm_attach(struct device *parent, struct device *self, void *aux)
1336 {
1337 struct drm_softc *sc = (struct drm_softc *)self;
1338 struct drm_attach_args *da = aux;
1339 struct drm_device *dev = da->drm;
1340 int ret;
1341
1342 if (drm_refcnt == 0) {
1343 drm_linux_init();
1344 drm_core_init();
1345 drm_buddy_module_init();
1346 }
1347 drm_refcnt++;
1348
1349 if (dev == NULL) {
1350 dev = malloc(sizeof(struct drm_device), M_DRM,
1351 M_WAITOK | M_ZERO);
1352 sc->sc_allocated = 1;
1353 }
1354
1355 sc->sc_drm = dev;
1356
1357 kref_init(&dev->ref);
1358 dev->dev = self;
1359 dev->dev_private = parent;
1360 dev->driver = da->driver;
1361
1362 INIT_LIST_HEAD(&dev->managed.resources);
1363 mtx_init(&dev->managed.lock, IPL_TTY);
1364
1365 /* no per-device feature limits by default */
1366 dev->driver_features = ~0u;
1367
1368 dev->dmat = da->dmat;
1369 dev->bst = da->bst;
1370 dev->unique = da->busid;
1371
1372 if (da->pa) {
1373 struct pci_attach_args *pa = da->pa;
1374 pcireg_t subsys;
1375
1376 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag,
1377 PCI_SUBSYS_ID_REG);
1378
1379 dev->pdev = &dev->_pdev;
1380 dev->pdev->vendor = PCI_VENDOR(pa->pa_id);
1381 dev->pdev->device = PCI_PRODUCT(pa->pa_id);
1382 dev->pdev->subsystem_vendor = PCI_VENDOR(subsys);
1383 dev->pdev->subsystem_device = PCI_PRODUCT(subsys);
1384 dev->pdev->revision = PCI_REVISION(pa->pa_class);
1385 dev->pdev->class = (PCI_CLASS(pa->pa_class) << 16) |
1386 (PCI_SUBCLASS(pa->pa_class) << 8) |
1387 PCI_INTERFACE(pa->pa_class);
1388
1389 dev->pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function);
1390 dev->pdev->bus = &dev->pdev->_bus;
1391 dev->pdev->bus->pc = pa->pa_pc;
1392 dev->pdev->bus->number = pa->pa_bus;
1393 dev->pdev->bus->domain_nr = pa->pa_domain;
1394 dev->pdev->bus->bridgetag = pa->pa_bridgetag;
1395
1396 if (pa->pa_bridgetag != NULL) {
1397 dev->pdev->bus->self = malloc(sizeof(struct pci_dev),
1398 M_DRM, M_WAITOK | M_ZERO);
1399 dev->pdev->bus->self->pc = pa->pa_pc;
1400 dev->pdev->bus->self->tag = *pa->pa_bridgetag;
1401 }
1402
1403 dev->pdev->pc = pa->pa_pc;
1404 dev->pdev->tag = pa->pa_tag;
1405 dev->pdev->pci = (struct pci_softc *)parent->dv_parent;
1406 dev->pdev->_dev = parent;
1407
1408 #ifdef CONFIG_ACPI
1409 dev->pdev->dev.node = acpi_find_pci(pa->pa_pc, pa->pa_tag);
1410 aml_register_notify(dev->pdev->dev.node, NULL,
1411 drm_linux_acpi_notify, NULL, ACPIDEV_NOPOLL);
1412 #endif
1413 }
1414
1415 mtx_init(&dev->quiesce_mtx, IPL_NONE);
1416 mtx_init(&dev->event_lock, IPL_TTY);
1417 rw_init(&dev->struct_mutex, "drmdevlk");
1418 rw_init(&dev->filelist_mutex, "drmflist");
1419 rw_init(&dev->clientlist_mutex, "drmclist");
1420 rw_init(&dev->master_mutex, "drmmast");
1421
1422 ret = drmm_add_action(dev, drm_dev_init_release, NULL);
1423 if (ret)
1424 goto error;
1425
1426 SPLAY_INIT(&dev->files);
1427 INIT_LIST_HEAD(&dev->filelist_internal);
1428 INIT_LIST_HEAD(&dev->clientlist);
1429 INIT_LIST_HEAD(&dev->vblank_event_list);
1430
1431 if (drm_core_check_feature(dev, DRIVER_RENDER)) {
1432 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
1433 if (ret)
1434 goto error;
1435 }
1436
1437 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY);
1438 if (ret)
1439 goto error;
1440
1441 #ifdef CONFIG_DRM_LEGACY
1442 if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
1443 #if IS_ENABLED(CONFIG_AGP)
1444 if (da->is_agp)
1445 dev->agp = drm_agp_init();
1446 #endif
1447 if (dev->agp != NULL) {
1448 if (drm_mtrr_add(dev->agp->info.ai_aperture_base,
1449 dev->agp->info.ai_aperture_size, DRM_MTRR_WC) == 0)
1450 dev->agp->mtrr = 1;
1451 }
1452 }
1453 #endif
1454
1455 if (dev->driver->gem_size > 0) {
1456 KASSERT(dev->driver->gem_size >= sizeof(struct drm_gem_object));
1457 /* XXX unique name */
1458 pool_init(&dev->objpl, dev->driver->gem_size, 0, IPL_NONE, 0,
1459 "drmobjpl", NULL);
1460 }
1461
1462 if (drm_core_check_feature(dev, DRIVER_GEM)) {
1463 ret = drm_gem_init(dev);
1464 if (ret) {
1465 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
1466 goto error;
1467 }
1468 }
1469
1470 drmm_add_final_kfree(dev, dev);
1471
1472 printf("\n");
1473 return;
1474
1475 error:
1476 drm_managed_release(dev);
1477 dev->dev_private = NULL;
1478 }
1479
1480 int
drm_detach(struct device * self,int flags)1481 drm_detach(struct device *self, int flags)
1482 {
1483 struct drm_softc *sc = (struct drm_softc *)self;
1484 struct drm_device *dev = sc->sc_drm;
1485
1486 drm_refcnt--;
1487 if (drm_refcnt == 0) {
1488 drm_buddy_module_exit();
1489 drm_core_exit();
1490 drm_linux_exit();
1491 }
1492
1493 drm_lastclose(dev);
1494
1495 if (drm_core_check_feature(dev, DRIVER_GEM)) {
1496 if (dev->driver->gem_size > 0)
1497 pool_destroy(&dev->objpl);
1498 }
1499
1500 #ifdef CONFIG_DRM_LEGACY
1501 if (dev->agp && dev->agp->mtrr) {
1502 int retcode;
1503
1504 retcode = drm_mtrr_del(0, dev->agp->info.ai_aperture_base,
1505 dev->agp->info.ai_aperture_size, DRM_MTRR_WC);
1506 DRM_DEBUG("mtrr_del = %d", retcode);
1507 }
1508
1509 free(dev->agp, M_DRM, 0);
1510 #endif
1511 if (dev->pdev && dev->pdev->bus)
1512 free(dev->pdev->bus->self, M_DRM, sizeof(struct pci_dev));
1513
1514 if (sc->sc_allocated)
1515 free(dev, M_DRM, sizeof(struct drm_device));
1516
1517 return 0;
1518 }
1519
1520 void
drm_quiesce(struct drm_device * dev)1521 drm_quiesce(struct drm_device *dev)
1522 {
1523 mtx_enter(&dev->quiesce_mtx);
1524 dev->quiesce = 1;
1525 while (dev->quiesce_count > 0) {
1526 msleep_nsec(&dev->quiesce_count, &dev->quiesce_mtx,
1527 PZERO, "drmqui", INFSLP);
1528 }
1529 mtx_leave(&dev->quiesce_mtx);
1530 }
1531
1532 void
drm_wakeup(struct drm_device * dev)1533 drm_wakeup(struct drm_device *dev)
1534 {
1535 mtx_enter(&dev->quiesce_mtx);
1536 dev->quiesce = 0;
1537 wakeup(&dev->quiesce);
1538 mtx_leave(&dev->quiesce_mtx);
1539 }
1540
1541 int
drm_activate(struct device * self,int act)1542 drm_activate(struct device *self, int act)
1543 {
1544 struct drm_softc *sc = (struct drm_softc *)self;
1545 struct drm_device *dev = sc->sc_drm;
1546
1547 switch (act) {
1548 case DVACT_QUIESCE:
1549 #ifdef CONFIG_ACPI
1550 if (acpi_softc) {
1551 switch (acpi_softc->sc_state) {
1552 case ACPI_STATE_S0:
1553 pm_suspend_target_state = PM_SUSPEND_TO_IDLE;
1554 break;
1555 case ACPI_STATE_S3:
1556 pm_suspend_target_state = PM_SUSPEND_MEM;
1557 break;
1558 }
1559 }
1560 #else
1561 pm_suspend_target_state = PM_SUSPEND_TO_IDLE;
1562 #endif
1563 drm_quiesce(dev);
1564 break;
1565 case DVACT_WAKEUP:
1566 drm_wakeup(dev);
1567 pm_suspend_target_state = PM_SUSPEND_ON;
1568 break;
1569 }
1570
1571 return (0);
1572 }
1573
1574 const struct cfattach drm_ca = {
1575 sizeof(struct drm_softc), drm_probe, drm_attach,
1576 drm_detach, drm_activate
1577 };
1578
1579 struct cfdriver drm_cd = {
1580 NULL, "drm", DV_DULL
1581 };
1582
1583 const struct pci_device_id *
drm_find_description(int vendor,int device,const struct pci_device_id * idlist)1584 drm_find_description(int vendor, int device, const struct pci_device_id *idlist)
1585 {
1586 int i = 0;
1587
1588 for (i = 0; idlist[i].vendor != 0; i++) {
1589 if ((idlist[i].vendor == vendor) &&
1590 (idlist[i].device == device ||
1591 idlist[i].device == PCI_ANY_ID) &&
1592 (idlist[i].subvendor == PCI_ANY_ID) &&
1593 (idlist[i].subdevice == PCI_ANY_ID))
1594 return &idlist[i];
1595 }
1596 return NULL;
1597 }
1598
1599 int
drm_file_cmp(struct drm_file * f1,struct drm_file * f2)1600 drm_file_cmp(struct drm_file *f1, struct drm_file *f2)
1601 {
1602 return (f1->fminor < f2->fminor ? -1 : f1->fminor > f2->fminor);
1603 }
1604
1605 SPLAY_GENERATE(drm_file_tree, drm_file, link, drm_file_cmp);
1606
1607 struct drm_file *
drm_find_file_by_minor(struct drm_device * dev,int minor)1608 drm_find_file_by_minor(struct drm_device *dev, int minor)
1609 {
1610 struct drm_file key;
1611
1612 key.fminor = minor;
1613 return (SPLAY_FIND(drm_file_tree, &dev->files, &key));
1614 }
1615
1616 struct drm_device *
drm_get_device_from_kdev(dev_t kdev)1617 drm_get_device_from_kdev(dev_t kdev)
1618 {
1619 int unit = minor(kdev) & ((1 << CLONE_SHIFT) - 1);
1620 /* render */
1621 if (unit >= 128)
1622 unit -= 128;
1623 struct drm_softc *sc;
1624
1625 if (unit < drm_cd.cd_ndevs) {
1626 sc = (struct drm_softc *)drm_cd.cd_devs[unit];
1627 if (sc)
1628 return sc->sc_drm;
1629 }
1630
1631 return NULL;
1632 }
1633
1634 void
filt_drmdetach(struct knote * kn)1635 filt_drmdetach(struct knote *kn)
1636 {
1637 struct drm_device *dev = kn->kn_hook;
1638 int s;
1639
1640 s = spltty();
1641 klist_remove_locked(&dev->note, kn);
1642 splx(s);
1643 }
1644
1645 int
filt_drmkms(struct knote * kn,long hint)1646 filt_drmkms(struct knote *kn, long hint)
1647 {
1648 if (kn->kn_sfflags & hint)
1649 kn->kn_fflags |= hint;
1650 return (kn->kn_fflags != 0);
1651 }
1652
1653 void
filt_drmreaddetach(struct knote * kn)1654 filt_drmreaddetach(struct knote *kn)
1655 {
1656 struct drm_file *file_priv = kn->kn_hook;
1657 int s;
1658
1659 s = spltty();
1660 klist_remove_locked(&file_priv->rsel.si_note, kn);
1661 splx(s);
1662 }
1663
1664 int
filt_drmread(struct knote * kn,long hint)1665 filt_drmread(struct knote *kn, long hint)
1666 {
1667 struct drm_file *file_priv = kn->kn_hook;
1668 int val = 0;
1669
1670 if ((hint & NOTE_SUBMIT) == 0)
1671 mtx_enter(&file_priv->minor->dev->event_lock);
1672 val = !list_empty(&file_priv->event_list);
1673 if ((hint & NOTE_SUBMIT) == 0)
1674 mtx_leave(&file_priv->minor->dev->event_lock);
1675 return (val);
1676 }
1677
1678 const struct filterops drm_filtops = {
1679 .f_flags = FILTEROP_ISFD,
1680 .f_attach = NULL,
1681 .f_detach = filt_drmdetach,
1682 .f_event = filt_drmkms,
1683 };
1684
1685 const struct filterops drmread_filtops = {
1686 .f_flags = FILTEROP_ISFD,
1687 .f_attach = NULL,
1688 .f_detach = filt_drmreaddetach,
1689 .f_event = filt_drmread,
1690 };
1691
1692 int
drmkqfilter(dev_t kdev,struct knote * kn)1693 drmkqfilter(dev_t kdev, struct knote *kn)
1694 {
1695 struct drm_device *dev = NULL;
1696 struct drm_file *file_priv = NULL;
1697 int s;
1698
1699 dev = drm_get_device_from_kdev(kdev);
1700 if (dev == NULL || dev->dev_private == NULL)
1701 return (ENXIO);
1702
1703 switch (kn->kn_filter) {
1704 case EVFILT_READ:
1705 mutex_lock(&dev->struct_mutex);
1706 file_priv = drm_find_file_by_minor(dev, minor(kdev));
1707 mutex_unlock(&dev->struct_mutex);
1708 if (file_priv == NULL)
1709 return (ENXIO);
1710
1711 kn->kn_fop = &drmread_filtops;
1712 kn->kn_hook = file_priv;
1713
1714 s = spltty();
1715 klist_insert_locked(&file_priv->rsel.si_note, kn);
1716 splx(s);
1717 break;
1718 case EVFILT_DEVICE:
1719 kn->kn_fop = &drm_filtops;
1720 kn->kn_hook = dev;
1721
1722 s = spltty();
1723 klist_insert_locked(&dev->note, kn);
1724 splx(s);
1725 break;
1726 default:
1727 return (EINVAL);
1728 }
1729
1730 return (0);
1731 }
1732
1733 int
drmopen(dev_t kdev,int flags,int fmt,struct proc * p)1734 drmopen(dev_t kdev, int flags, int fmt, struct proc *p)
1735 {
1736 struct drm_device *dev = NULL;
1737 struct drm_file *file_priv;
1738 struct drm_minor *dm;
1739 int ret = 0;
1740 int dminor, realminor, minor_type;
1741
1742 dev = drm_get_device_from_kdev(kdev);
1743 if (dev == NULL || dev->dev_private == NULL)
1744 return (ENXIO);
1745
1746 DRM_DEBUG("open_count = %d\n", atomic_read(&dev->open_count));
1747
1748 if (flags & O_EXCL)
1749 return (EBUSY); /* No exclusive opens */
1750
1751 if (drm_dev_needs_global_mutex(dev))
1752 mutex_lock(&drm_global_mutex);
1753
1754 atomic_fetch_inc(&dev->open_count);
1755
1756 dminor = minor(kdev);
1757 realminor = dminor & ((1 << CLONE_SHIFT) - 1);
1758 if (realminor < 64)
1759 minor_type = DRM_MINOR_PRIMARY;
1760 else if (realminor >= 128 && realminor < 192)
1761 minor_type = DRM_MINOR_RENDER;
1762 else {
1763 ret = ENXIO;
1764 goto err;
1765 }
1766
1767 dm = *drm_minor_get_slot(dev, minor_type);
1768 if (dm == NULL) {
1769 ret = ENXIO;
1770 goto err;
1771 }
1772 dm->index = minor(kdev);
1773
1774 file_priv = drm_file_alloc(dm);
1775 if (IS_ERR(file_priv)) {
1776 ret = ENOMEM;
1777 goto err;
1778 }
1779
1780 /* first opener automatically becomes master */
1781 if (drm_is_primary_client(file_priv)) {
1782 ret = drm_master_open(file_priv);
1783 if (ret != 0)
1784 goto out_file_free;
1785 }
1786
1787 file_priv->filp = (void *)file_priv;
1788 file_priv->fminor = minor(kdev);
1789
1790 mutex_lock(&dev->filelist_mutex);
1791 SPLAY_INSERT(drm_file_tree, &dev->files, file_priv);
1792 mutex_unlock(&dev->filelist_mutex);
1793
1794 if (drm_dev_needs_global_mutex(dev))
1795 mutex_unlock(&drm_global_mutex);
1796
1797 return 0;
1798
1799 out_file_free:
1800 drm_file_free(file_priv);
1801 err:
1802 atomic_dec(&dev->open_count);
1803 if (drm_dev_needs_global_mutex(dev))
1804 mutex_unlock(&drm_global_mutex);
1805 return (ret);
1806 }
1807
1808 int
drmclose(dev_t kdev,int flags,int fmt,struct proc * p)1809 drmclose(dev_t kdev, int flags, int fmt, struct proc *p)
1810 {
1811 struct drm_device *dev = drm_get_device_from_kdev(kdev);
1812 struct drm_file *file_priv;
1813 int retcode = 0;
1814
1815 if (dev == NULL)
1816 return (ENXIO);
1817
1818 if (drm_dev_needs_global_mutex(dev))
1819 mutex_lock(&drm_global_mutex);
1820
1821 DRM_DEBUG("open_count = %d\n", atomic_read(&dev->open_count));
1822
1823 mutex_lock(&dev->filelist_mutex);
1824 file_priv = drm_find_file_by_minor(dev, minor(kdev));
1825 if (file_priv == NULL) {
1826 DRM_ERROR("can't find authenticator\n");
1827 retcode = EINVAL;
1828 mutex_unlock(&dev->filelist_mutex);
1829 goto done;
1830 }
1831
1832 SPLAY_REMOVE(drm_file_tree, &dev->files, file_priv);
1833 mutex_unlock(&dev->filelist_mutex);
1834 drm_file_free(file_priv);
1835 done:
1836 if (atomic_dec_and_test(&dev->open_count))
1837 drm_lastclose(dev);
1838
1839 if (drm_dev_needs_global_mutex(dev))
1840 mutex_unlock(&drm_global_mutex);
1841
1842 return (retcode);
1843 }
1844
1845 int
drmread(dev_t kdev,struct uio * uio,int ioflag)1846 drmread(dev_t kdev, struct uio *uio, int ioflag)
1847 {
1848 struct drm_device *dev = drm_get_device_from_kdev(kdev);
1849 struct drm_file *file_priv;
1850 struct drm_pending_event *ev;
1851 int error = 0;
1852
1853 if (dev == NULL)
1854 return (ENXIO);
1855
1856 mutex_lock(&dev->filelist_mutex);
1857 file_priv = drm_find_file_by_minor(dev, minor(kdev));
1858 mutex_unlock(&dev->filelist_mutex);
1859 if (file_priv == NULL)
1860 return (ENXIO);
1861
1862 /*
1863 * The semantics are a little weird here. We will wait until we
1864 * have events to process, but as soon as we have events we will
1865 * only deliver as many as we have.
1866 * Note that events are atomic, if the read buffer will not fit in
1867 * a whole event, we won't read any of it out.
1868 */
1869 mtx_enter(&dev->event_lock);
1870 while (error == 0 && list_empty(&file_priv->event_list)) {
1871 if (ioflag & IO_NDELAY) {
1872 mtx_leave(&dev->event_lock);
1873 return (EAGAIN);
1874 }
1875 error = msleep_nsec(&file_priv->event_wait, &dev->event_lock,
1876 PWAIT | PCATCH, "drmread", INFSLP);
1877 }
1878 if (error) {
1879 mtx_leave(&dev->event_lock);
1880 return (error);
1881 }
1882 while (drm_dequeue_event(dev, file_priv, uio->uio_resid, &ev)) {
1883 MUTEX_ASSERT_UNLOCKED(&dev->event_lock);
1884 /* XXX we always destroy the event on error. */
1885 error = uiomove(ev->event, ev->event->length, uio);
1886 kfree(ev);
1887 if (error)
1888 break;
1889 mtx_enter(&dev->event_lock);
1890 }
1891 MUTEX_ASSERT_UNLOCKED(&dev->event_lock);
1892
1893 return (error);
1894 }
1895
1896 /*
1897 * Deqeue an event from the file priv in question. returning 1 if an
1898 * event was found. We take the resid from the read as a parameter because
1899 * we will only dequeue and event if the read buffer has space to fit the
1900 * entire thing.
1901 *
1902 * We are called locked, but we will *unlock* the queue on return so that
1903 * we may sleep to copyout the event.
1904 */
1905 int
drm_dequeue_event(struct drm_device * dev,struct drm_file * file_priv,size_t resid,struct drm_pending_event ** out)1906 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv,
1907 size_t resid, struct drm_pending_event **out)
1908 {
1909 struct drm_pending_event *e = NULL;
1910 int gotone = 0;
1911
1912 MUTEX_ASSERT_LOCKED(&dev->event_lock);
1913
1914 *out = NULL;
1915 if (list_empty(&file_priv->event_list))
1916 goto out;
1917 e = list_first_entry(&file_priv->event_list,
1918 struct drm_pending_event, link);
1919 if (e->event->length > resid)
1920 goto out;
1921
1922 file_priv->event_space += e->event->length;
1923 list_del(&e->link);
1924 *out = e;
1925 gotone = 1;
1926
1927 out:
1928 mtx_leave(&dev->event_lock);
1929
1930 return (gotone);
1931 }
1932
1933 paddr_t
drmmmap(dev_t kdev,off_t offset,int prot)1934 drmmmap(dev_t kdev, off_t offset, int prot)
1935 {
1936 return -1;
1937 }
1938
1939 struct drm_dmamem *
drm_dmamem_alloc(bus_dma_tag_t dmat,bus_size_t size,bus_size_t alignment,int nsegments,bus_size_t maxsegsz,int mapflags,int loadflags)1940 drm_dmamem_alloc(bus_dma_tag_t dmat, bus_size_t size, bus_size_t alignment,
1941 int nsegments, bus_size_t maxsegsz, int mapflags, int loadflags)
1942 {
1943 struct drm_dmamem *mem;
1944 size_t strsize;
1945 /*
1946 * segs is the last member of the struct since we modify the size
1947 * to allow extra segments if more than one are allowed.
1948 */
1949 strsize = sizeof(*mem) + (sizeof(bus_dma_segment_t) * (nsegments - 1));
1950 mem = malloc(strsize, M_DRM, M_NOWAIT | M_ZERO);
1951 if (mem == NULL)
1952 return (NULL);
1953
1954 mem->size = size;
1955
1956 if (bus_dmamap_create(dmat, size, nsegments, maxsegsz, 0,
1957 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &mem->map) != 0)
1958 goto strfree;
1959
1960 if (bus_dmamem_alloc(dmat, size, alignment, 0, mem->segs, nsegments,
1961 &mem->nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO) != 0)
1962 goto destroy;
1963
1964 if (bus_dmamem_map(dmat, mem->segs, mem->nsegs, size,
1965 &mem->kva, BUS_DMA_NOWAIT | mapflags) != 0)
1966 goto free;
1967
1968 if (bus_dmamap_load(dmat, mem->map, mem->kva, size,
1969 NULL, BUS_DMA_NOWAIT | loadflags) != 0)
1970 goto unmap;
1971
1972 return (mem);
1973
1974 unmap:
1975 bus_dmamem_unmap(dmat, mem->kva, size);
1976 free:
1977 bus_dmamem_free(dmat, mem->segs, mem->nsegs);
1978 destroy:
1979 bus_dmamap_destroy(dmat, mem->map);
1980 strfree:
1981 free(mem, M_DRM, 0);
1982
1983 return (NULL);
1984 }
1985
1986 void
drm_dmamem_free(bus_dma_tag_t dmat,struct drm_dmamem * mem)1987 drm_dmamem_free(bus_dma_tag_t dmat, struct drm_dmamem *mem)
1988 {
1989 if (mem == NULL)
1990 return;
1991
1992 bus_dmamap_unload(dmat, mem->map);
1993 bus_dmamem_unmap(dmat, mem->kva, mem->size);
1994 bus_dmamem_free(dmat, mem->segs, mem->nsegs);
1995 bus_dmamap_destroy(dmat, mem->map);
1996 free(mem, M_DRM, 0);
1997 }
1998
1999 struct drm_dma_handle *
drm_pci_alloc(struct drm_device * dev,size_t size,size_t align)2000 drm_pci_alloc(struct drm_device *dev, size_t size, size_t align)
2001 {
2002 struct drm_dma_handle *dmah;
2003
2004 dmah = malloc(sizeof(*dmah), M_DRM, M_WAITOK);
2005 dmah->mem = drm_dmamem_alloc(dev->dmat, size, align, 1, size,
2006 BUS_DMA_NOCACHE, 0);
2007 if (dmah->mem == NULL) {
2008 free(dmah, M_DRM, sizeof(*dmah));
2009 return NULL;
2010 }
2011 dmah->busaddr = dmah->mem->segs[0].ds_addr;
2012 dmah->size = dmah->mem->size;
2013 dmah->vaddr = dmah->mem->kva;
2014 return (dmah);
2015 }
2016
2017 void
drm_pci_free(struct drm_device * dev,struct drm_dma_handle * dmah)2018 drm_pci_free(struct drm_device *dev, struct drm_dma_handle *dmah)
2019 {
2020 if (dmah == NULL)
2021 return;
2022
2023 drm_dmamem_free(dev->dmat, dmah->mem);
2024 free(dmah, M_DRM, sizeof(*dmah));
2025 }
2026
2027 /*
2028 * Compute order. Can be made faster.
2029 */
2030 int
drm_order(unsigned long size)2031 drm_order(unsigned long size)
2032 {
2033 int order;
2034 unsigned long tmp;
2035
2036 for (order = 0, tmp = size; tmp >>= 1; ++order)
2037 ;
2038
2039 if (size & ~(1 << order))
2040 ++order;
2041
2042 return order;
2043 }
2044
2045 int
drm_getpciinfo(struct drm_device * dev,void * data,struct drm_file * file_priv)2046 drm_getpciinfo(struct drm_device *dev, void *data, struct drm_file *file_priv)
2047 {
2048 struct drm_pciinfo *info = data;
2049
2050 if (dev->pdev == NULL)
2051 return -ENOTTY;
2052
2053 info->domain = dev->pdev->bus->domain_nr;
2054 info->bus = dev->pdev->bus->number;
2055 info->dev = PCI_SLOT(dev->pdev->devfn);
2056 info->func = PCI_FUNC(dev->pdev->devfn);
2057 info->vendor_id = dev->pdev->vendor;
2058 info->device_id = dev->pdev->device;
2059 info->subvendor_id = dev->pdev->subsystem_vendor;
2060 info->subdevice_id = dev->pdev->subsystem_device;
2061 info->revision_id = 0;
2062
2063 return 0;
2064 }
2065