drm_stub.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /**
  2. * \file drm_stub.h
  3. * Stub support
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. */
  7. /*
  8. * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
  9. *
  10. * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
  11. * All Rights Reserved.
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice (including the next
  21. * paragraph) shall be included in all copies or substantial portions of the
  22. * Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  28. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  29. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. * DEALINGS IN THE SOFTWARE.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/slab.h>
  35. #include "drmP.h"
  36. #include "drm_core.h"
  37. unsigned int drm_debug = 0; /* 1 to enable debug output */
  38. EXPORT_SYMBOL(drm_debug);
  39. MODULE_AUTHOR(CORE_AUTHOR);
  40. MODULE_DESCRIPTION(CORE_DESC);
  41. MODULE_LICENSE("GPL and additional rights");
  42. MODULE_PARM_DESC(debug, "Enable debug output");
  43. module_param_named(debug, drm_debug, int, 0600);
  44. struct idr drm_minors_idr;
  45. struct class *drm_class;
  46. struct proc_dir_entry *drm_proc_root;
  47. struct dentry *drm_debugfs_root;
  48. void drm_ut_debug_printk(unsigned int request_level,
  49. const char *prefix,
  50. const char *function_name,
  51. const char *format, ...)
  52. {
  53. va_list args;
  54. if (drm_debug & request_level) {
  55. if (function_name)
  56. printk(KERN_DEBUG "[%s:%s], ", prefix, function_name);
  57. va_start(args, format);
  58. vprintk(format, args);
  59. va_end(args);
  60. }
  61. }
  62. EXPORT_SYMBOL(drm_ut_debug_printk);
  63. static int drm_minor_get_id(struct drm_device *dev, int type)
  64. {
  65. int new_id;
  66. int ret;
  67. int base = 0, limit = 63;
  68. if (type == DRM_MINOR_CONTROL) {
  69. base += 64;
  70. limit = base + 127;
  71. } else if (type == DRM_MINOR_RENDER) {
  72. base += 128;
  73. limit = base + 255;
  74. }
  75. again:
  76. if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
  77. DRM_ERROR("Out of memory expanding drawable idr\n");
  78. return -ENOMEM;
  79. }
  80. mutex_lock(&dev->struct_mutex);
  81. ret = idr_get_new_above(&drm_minors_idr, NULL,
  82. base, &new_id);
  83. mutex_unlock(&dev->struct_mutex);
  84. if (ret == -EAGAIN) {
  85. goto again;
  86. } else if (ret) {
  87. return ret;
  88. }
  89. if (new_id >= limit) {
  90. idr_remove(&drm_minors_idr, new_id);
  91. return -EINVAL;
  92. }
  93. return new_id;
  94. }
  95. struct drm_master *drm_master_create(struct drm_minor *minor)
  96. {
  97. struct drm_master *master;
  98. master = kzalloc(sizeof(*master), GFP_KERNEL);
  99. if (!master)
  100. return NULL;
  101. kref_init(&master->refcount);
  102. spin_lock_init(&master->lock.spinlock);
  103. init_waitqueue_head(&master->lock.lock_queue);
  104. drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
  105. INIT_LIST_HEAD(&master->magicfree);
  106. master->minor = minor;
  107. list_add_tail(&master->head, &minor->master_list);
  108. return master;
  109. }
  110. struct drm_master *drm_master_get(struct drm_master *master)
  111. {
  112. kref_get(&master->refcount);
  113. return master;
  114. }
  115. EXPORT_SYMBOL(drm_master_get);
  116. static void drm_master_destroy(struct kref *kref)
  117. {
  118. struct drm_master *master = container_of(kref, struct drm_master, refcount);
  119. struct drm_magic_entry *pt, *next;
  120. struct drm_device *dev = master->minor->dev;
  121. struct drm_map_list *r_list, *list_temp;
  122. list_del(&master->head);
  123. if (dev->driver->master_destroy)
  124. dev->driver->master_destroy(dev, master);
  125. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
  126. if (r_list->master == master) {
  127. drm_rmmap_locked(dev, r_list->map);
  128. r_list = NULL;
  129. }
  130. }
  131. if (master->unique) {
  132. kfree(master->unique);
  133. master->unique = NULL;
  134. master->unique_len = 0;
  135. }
  136. list_for_each_entry_safe(pt, next, &master->magicfree, head) {
  137. list_del(&pt->head);
  138. drm_ht_remove_item(&master->magiclist, &pt->hash_item);
  139. kfree(pt);
  140. }
  141. drm_ht_remove(&master->magiclist);
  142. kfree(master);
  143. }
  144. void drm_master_put(struct drm_master **master)
  145. {
  146. kref_put(&(*master)->refcount, drm_master_destroy);
  147. *master = NULL;
  148. }
  149. EXPORT_SYMBOL(drm_master_put);
  150. int drm_setmaster_ioctl(struct drm_device *dev, void *data,
  151. struct drm_file *file_priv)
  152. {
  153. int ret = 0;
  154. if (file_priv->is_master)
  155. return 0;
  156. if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
  157. return -EINVAL;
  158. if (!file_priv->master)
  159. return -EINVAL;
  160. if (!file_priv->minor->master &&
  161. file_priv->minor->master != file_priv->master) {
  162. mutex_lock(&dev->struct_mutex);
  163. file_priv->minor->master = drm_master_get(file_priv->master);
  164. file_priv->is_master = 1;
  165. if (dev->driver->master_set) {
  166. ret = dev->driver->master_set(dev, file_priv, false);
  167. if (unlikely(ret != 0)) {
  168. file_priv->is_master = 0;
  169. drm_master_put(&file_priv->minor->master);
  170. }
  171. }
  172. mutex_unlock(&dev->struct_mutex);
  173. }
  174. return 0;
  175. }
  176. int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
  177. struct drm_file *file_priv)
  178. {
  179. if (!file_priv->is_master)
  180. return -EINVAL;
  181. if (!file_priv->minor->master)
  182. return -EINVAL;
  183. mutex_lock(&dev->struct_mutex);
  184. if (dev->driver->master_drop)
  185. dev->driver->master_drop(dev, file_priv, false);
  186. drm_master_put(&file_priv->minor->master);
  187. file_priv->is_master = 0;
  188. mutex_unlock(&dev->struct_mutex);
  189. return 0;
  190. }
  191. static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev,
  192. const struct pci_device_id *ent,
  193. struct drm_driver *driver)
  194. {
  195. int retcode;
  196. INIT_LIST_HEAD(&dev->filelist);
  197. INIT_LIST_HEAD(&dev->ctxlist);
  198. INIT_LIST_HEAD(&dev->vmalist);
  199. INIT_LIST_HEAD(&dev->maplist);
  200. INIT_LIST_HEAD(&dev->vblank_event_list);
  201. spin_lock_init(&dev->count_lock);
  202. spin_lock_init(&dev->drw_lock);
  203. spin_lock_init(&dev->event_lock);
  204. init_timer(&dev->timer);
  205. mutex_init(&dev->struct_mutex);
  206. mutex_init(&dev->ctxlist_mutex);
  207. idr_init(&dev->drw_idr);
  208. dev->pdev = pdev;
  209. dev->pci_device = pdev->device;
  210. dev->pci_vendor = pdev->vendor;
  211. #ifdef __alpha__
  212. dev->hose = pdev->sysdata;
  213. #endif
  214. if (drm_ht_create(&dev->map_hash, 12)) {
  215. return -ENOMEM;
  216. }
  217. /* the DRM has 6 basic counters */
  218. dev->counters = 6;
  219. dev->types[0] = _DRM_STAT_LOCK;
  220. dev->types[1] = _DRM_STAT_OPENS;
  221. dev->types[2] = _DRM_STAT_CLOSES;
  222. dev->types[3] = _DRM_STAT_IOCTLS;
  223. dev->types[4] = _DRM_STAT_LOCKS;
  224. dev->types[5] = _DRM_STAT_UNLOCKS;
  225. dev->driver = driver;
  226. if (drm_core_has_AGP(dev)) {
  227. if (drm_device_is_agp(dev))
  228. dev->agp = drm_agp_init(dev);
  229. if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
  230. && (dev->agp == NULL)) {
  231. DRM_ERROR("Cannot initialize the agpgart module.\n");
  232. retcode = -EINVAL;
  233. goto error_out_unreg;
  234. }
  235. if (drm_core_has_MTRR(dev)) {
  236. if (dev->agp)
  237. dev->agp->agp_mtrr =
  238. mtrr_add(dev->agp->agp_info.aper_base,
  239. dev->agp->agp_info.aper_size *
  240. 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
  241. }
  242. }
  243. retcode = drm_ctxbitmap_init(dev);
  244. if (retcode) {
  245. DRM_ERROR("Cannot allocate memory for context bitmap.\n");
  246. goto error_out_unreg;
  247. }
  248. if (driver->driver_features & DRIVER_GEM) {
  249. retcode = drm_gem_init(dev);
  250. if (retcode) {
  251. DRM_ERROR("Cannot initialize graphics execution "
  252. "manager (GEM)\n");
  253. goto error_out_unreg;
  254. }
  255. }
  256. return 0;
  257. error_out_unreg:
  258. drm_lastclose(dev);
  259. return retcode;
  260. }
  261. /**
  262. * Get a secondary minor number.
  263. *
  264. * \param dev device data structure
  265. * \param sec-minor structure to hold the assigned minor
  266. * \return negative number on failure.
  267. *
  268. * Search an empty entry and initialize it to the given parameters, and
  269. * create the proc init entry via proc_init(). This routines assigns
  270. * minor numbers to secondary heads of multi-headed cards
  271. */
  272. static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
  273. {
  274. struct drm_minor *new_minor;
  275. int ret;
  276. int minor_id;
  277. DRM_DEBUG("\n");
  278. minor_id = drm_minor_get_id(dev, type);
  279. if (minor_id < 0)
  280. return minor_id;
  281. new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
  282. if (!new_minor) {
  283. ret = -ENOMEM;
  284. goto err_idr;
  285. }
  286. new_minor->type = type;
  287. new_minor->device = MKDEV(DRM_MAJOR, minor_id);
  288. new_minor->dev = dev;
  289. new_minor->index = minor_id;
  290. INIT_LIST_HEAD(&new_minor->master_list);
  291. idr_replace(&drm_minors_idr, new_minor, minor_id);
  292. if (type == DRM_MINOR_LEGACY) {
  293. ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
  294. if (ret) {
  295. DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
  296. goto err_mem;
  297. }
  298. } else
  299. new_minor->proc_root = NULL;
  300. #if defined(CONFIG_DEBUG_FS)
  301. ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
  302. if (ret) {
  303. DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
  304. goto err_g2;
  305. }
  306. #endif
  307. ret = drm_sysfs_device_add(new_minor);
  308. if (ret) {
  309. printk(KERN_ERR
  310. "DRM: Error sysfs_device_add.\n");
  311. goto err_g2;
  312. }
  313. *minor = new_minor;
  314. DRM_DEBUG("new minor assigned %d\n", minor_id);
  315. return 0;
  316. err_g2:
  317. if (new_minor->type == DRM_MINOR_LEGACY)
  318. drm_proc_cleanup(new_minor, drm_proc_root);
  319. err_mem:
  320. kfree(new_minor);
  321. err_idr:
  322. idr_remove(&drm_minors_idr, minor_id);
  323. *minor = NULL;
  324. return ret;
  325. }
  326. /**
  327. * Register.
  328. *
  329. * \param pdev - PCI device structure
  330. * \param ent entry from the PCI ID table with device type flags
  331. * \return zero on success or a negative number on failure.
  332. *
  333. * Attempt to gets inter module "drm" information. If we are first
  334. * then register the character device and inter module information.
  335. * Try and register, if we fail to register, backout previous work.
  336. */
  337. int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
  338. struct drm_driver *driver)
  339. {
  340. struct drm_device *dev;
  341. int ret;
  342. DRM_DEBUG("\n");
  343. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  344. if (!dev)
  345. return -ENOMEM;
  346. ret = pci_enable_device(pdev);
  347. if (ret)
  348. goto err_g1;
  349. pci_set_master(pdev);
  350. if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
  351. printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
  352. goto err_g2;
  353. }
  354. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  355. pci_set_drvdata(pdev, dev);
  356. ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
  357. if (ret)
  358. goto err_g2;
  359. }
  360. if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
  361. goto err_g3;
  362. if (dev->driver->load) {
  363. ret = dev->driver->load(dev, ent->driver_data);
  364. if (ret)
  365. goto err_g4;
  366. }
  367. /* setup the grouping for the legacy output */
  368. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  369. ret = drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
  370. if (ret)
  371. goto err_g4;
  372. }
  373. list_add_tail(&dev->driver_item, &driver->device_list);
  374. DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
  375. driver->name, driver->major, driver->minor, driver->patchlevel,
  376. driver->date, pci_name(pdev), dev->primary->index);
  377. return 0;
  378. err_g4:
  379. drm_put_minor(&dev->primary);
  380. err_g3:
  381. if (drm_core_check_feature(dev, DRIVER_MODESET))
  382. drm_put_minor(&dev->control);
  383. err_g2:
  384. pci_disable_device(pdev);
  385. err_g1:
  386. kfree(dev);
  387. return ret;
  388. }
  389. EXPORT_SYMBOL(drm_get_dev);
  390. /**
  391. * Put a secondary minor number.
  392. *
  393. * \param sec_minor - structure to be released
  394. * \return always zero
  395. *
  396. * Cleans up the proc resources. Not legal for this to be the
  397. * last minor released.
  398. *
  399. */
  400. int drm_put_minor(struct drm_minor **minor_p)
  401. {
  402. struct drm_minor *minor = *minor_p;
  403. DRM_DEBUG("release secondary minor %d\n", minor->index);
  404. if (minor->type == DRM_MINOR_LEGACY)
  405. drm_proc_cleanup(minor, drm_proc_root);
  406. #if defined(CONFIG_DEBUG_FS)
  407. drm_debugfs_cleanup(minor);
  408. #endif
  409. drm_sysfs_device_remove(minor);
  410. idr_remove(&drm_minors_idr, minor->index);
  411. kfree(minor);
  412. *minor_p = NULL;
  413. return 0;
  414. }
  415. /**
  416. * Called via drm_exit() at module unload time or when pci device is
  417. * unplugged.
  418. *
  419. * Cleans up all DRM device, calling drm_lastclose().
  420. *
  421. * \sa drm_init
  422. */
  423. void drm_put_dev(struct drm_device *dev)
  424. {
  425. struct drm_driver *driver;
  426. struct drm_map_list *r_list, *list_temp;
  427. DRM_DEBUG("\n");
  428. if (!dev) {
  429. DRM_ERROR("cleanup called no dev\n");
  430. return;
  431. }
  432. driver = dev->driver;
  433. drm_lastclose(dev);
  434. if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
  435. dev->agp && dev->agp->agp_mtrr >= 0) {
  436. int retval;
  437. retval = mtrr_del(dev->agp->agp_mtrr,
  438. dev->agp->agp_info.aper_base,
  439. dev->agp->agp_info.aper_size * 1024 * 1024);
  440. DRM_DEBUG("mtrr_del=%d\n", retval);
  441. }
  442. if (dev->driver->unload)
  443. dev->driver->unload(dev);
  444. if (drm_core_has_AGP(dev) && dev->agp) {
  445. kfree(dev->agp);
  446. dev->agp = NULL;
  447. }
  448. drm_vblank_cleanup(dev);
  449. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
  450. drm_rmmap(dev, r_list->map);
  451. drm_ht_remove(&dev->map_hash);
  452. drm_ctxbitmap_cleanup(dev);
  453. if (drm_core_check_feature(dev, DRIVER_MODESET))
  454. drm_put_minor(&dev->control);
  455. if (driver->driver_features & DRIVER_GEM)
  456. drm_gem_destroy(dev);
  457. drm_put_minor(&dev->primary);
  458. if (dev->devname) {
  459. kfree(dev->devname);
  460. dev->devname = NULL;
  461. }
  462. kfree(dev);
  463. }
  464. EXPORT_SYMBOL(drm_put_dev);