drm_stub.c 13 KB

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