vgaarb.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * vgaarb.c: Implements the VGA arbitration. For details refer to
  4. * Documentation/gpu/vgaarbiter.rst
  5. *
  6. * (C) Copyright 2005 Benjamin Herrenschmidt <[email protected]>
  7. * (C) Copyright 2007 Paulo R. Zanoni <[email protected]>
  8. * (C) Copyright 2007, 2009 Tiago Vignatti <[email protected]>
  9. */
  10. #define pr_fmt(fmt) "vgaarb: " fmt
  11. #define vgaarb_dbg(dev, fmt, arg...) dev_dbg(dev, "vgaarb: " fmt, ##arg)
  12. #define vgaarb_info(dev, fmt, arg...) dev_info(dev, "vgaarb: " fmt, ##arg)
  13. #define vgaarb_err(dev, fmt, arg...) dev_err(dev, "vgaarb: " fmt, ##arg)
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/pci.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/list.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/wait.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/poll.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/slab.h>
  26. #include <linux/screen_info.h>
  27. #include <linux/vt.h>
  28. #include <linux/console.h>
  29. #include <linux/acpi.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/vgaarb.h>
  32. static void vga_arbiter_notify_clients(void);
  33. /*
  34. * We keep a list of all vga devices in the system to speed
  35. * up the various operations of the arbiter
  36. */
  37. struct vga_device {
  38. struct list_head list;
  39. struct pci_dev *pdev;
  40. unsigned int decodes; /* what does it decodes */
  41. unsigned int owns; /* what does it owns */
  42. unsigned int locks; /* what does it locks */
  43. unsigned int io_lock_cnt; /* legacy IO lock count */
  44. unsigned int mem_lock_cnt; /* legacy MEM lock count */
  45. unsigned int io_norm_cnt; /* normal IO count */
  46. unsigned int mem_norm_cnt; /* normal MEM count */
  47. bool bridge_has_one_vga;
  48. bool is_firmware_default; /* device selected by firmware */
  49. unsigned int (*set_decode)(struct pci_dev *pdev, bool decode);
  50. };
  51. static LIST_HEAD(vga_list);
  52. static int vga_count, vga_decode_count;
  53. static bool vga_arbiter_used;
  54. static DEFINE_SPINLOCK(vga_lock);
  55. static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
  56. static const char *vga_iostate_to_str(unsigned int iostate)
  57. {
  58. /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
  59. iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  60. switch (iostate) {
  61. case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
  62. return "io+mem";
  63. case VGA_RSRC_LEGACY_IO:
  64. return "io";
  65. case VGA_RSRC_LEGACY_MEM:
  66. return "mem";
  67. }
  68. return "none";
  69. }
  70. static int vga_str_to_iostate(char *buf, int str_size, int *io_state)
  71. {
  72. /* we could in theory hand out locks on IO and mem
  73. * separately to userspace but it can cause deadlocks */
  74. if (strncmp(buf, "none", 4) == 0) {
  75. *io_state = VGA_RSRC_NONE;
  76. return 1;
  77. }
  78. /* XXX We're not chekcing the str_size! */
  79. if (strncmp(buf, "io+mem", 6) == 0)
  80. goto both;
  81. else if (strncmp(buf, "io", 2) == 0)
  82. goto both;
  83. else if (strncmp(buf, "mem", 3) == 0)
  84. goto both;
  85. return 0;
  86. both:
  87. *io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  88. return 1;
  89. }
  90. /* this is only used a cookie - it should not be dereferenced */
  91. static struct pci_dev *vga_default;
  92. /* Find somebody in our list */
  93. static struct vga_device *vgadev_find(struct pci_dev *pdev)
  94. {
  95. struct vga_device *vgadev;
  96. list_for_each_entry(vgadev, &vga_list, list)
  97. if (pdev == vgadev->pdev)
  98. return vgadev;
  99. return NULL;
  100. }
  101. /**
  102. * vga_default_device - return the default VGA device, for vgacon
  103. *
  104. * This can be defined by the platform. The default implementation
  105. * is rather dumb and will probably only work properly on single
  106. * vga card setups and/or x86 platforms.
  107. *
  108. * If your VGA default device is not PCI, you'll have to return
  109. * NULL here. In this case, I assume it will not conflict with
  110. * any PCI card. If this is not true, I'll have to define two archs
  111. * hooks for enabling/disabling the VGA default device if that is
  112. * possible. This may be a problem with real _ISA_ VGA cards, in
  113. * addition to a PCI one. I don't know at this point how to deal
  114. * with that card. Can theirs IOs be disabled at all ? If not, then
  115. * I suppose it's a matter of having the proper arch hook telling
  116. * us about it, so we basically never allow anybody to succeed a
  117. * vga_get()...
  118. */
  119. struct pci_dev *vga_default_device(void)
  120. {
  121. return vga_default;
  122. }
  123. EXPORT_SYMBOL_GPL(vga_default_device);
  124. void vga_set_default_device(struct pci_dev *pdev)
  125. {
  126. if (vga_default == pdev)
  127. return;
  128. pci_dev_put(vga_default);
  129. vga_default = pci_dev_get(pdev);
  130. }
  131. /**
  132. * vga_remove_vgacon - deactivete vga console
  133. *
  134. * Unbind and unregister vgacon in case pdev is the default vga
  135. * device. Can be called by gpu drivers on initialization to make
  136. * sure vga register access done by vgacon will not disturb the
  137. * device.
  138. *
  139. * @pdev: pci device.
  140. */
  141. #if !defined(CONFIG_VGA_CONSOLE)
  142. int vga_remove_vgacon(struct pci_dev *pdev)
  143. {
  144. return 0;
  145. }
  146. #elif !defined(CONFIG_DUMMY_CONSOLE)
  147. int vga_remove_vgacon(struct pci_dev *pdev)
  148. {
  149. return -ENODEV;
  150. }
  151. #else
  152. int vga_remove_vgacon(struct pci_dev *pdev)
  153. {
  154. int ret = 0;
  155. if (pdev != vga_default)
  156. return 0;
  157. vgaarb_info(&pdev->dev, "deactivate vga console\n");
  158. console_lock();
  159. if (con_is_bound(&vga_con))
  160. ret = do_take_over_console(&dummy_con, 0,
  161. MAX_NR_CONSOLES - 1, 1);
  162. if (ret == 0) {
  163. ret = do_unregister_con_driver(&vga_con);
  164. /* Ignore "already unregistered". */
  165. if (ret == -ENODEV)
  166. ret = 0;
  167. }
  168. console_unlock();
  169. return ret;
  170. }
  171. #endif
  172. EXPORT_SYMBOL(vga_remove_vgacon);
  173. /* If we don't ever use VGA arb we should avoid
  174. turning off anything anywhere due to old X servers getting
  175. confused about the boot device not being VGA */
  176. static void vga_check_first_use(void)
  177. {
  178. /* we should inform all GPUs in the system that
  179. * VGA arb has occurred and to try and disable resources
  180. * if they can */
  181. if (!vga_arbiter_used) {
  182. vga_arbiter_used = true;
  183. vga_arbiter_notify_clients();
  184. }
  185. }
  186. static struct vga_device *__vga_tryget(struct vga_device *vgadev,
  187. unsigned int rsrc)
  188. {
  189. struct device *dev = &vgadev->pdev->dev;
  190. unsigned int wants, legacy_wants, match;
  191. struct vga_device *conflict;
  192. unsigned int pci_bits;
  193. u32 flags = 0;
  194. /* Account for "normal" resources to lock. If we decode the legacy,
  195. * counterpart, we need to request it as well
  196. */
  197. if ((rsrc & VGA_RSRC_NORMAL_IO) &&
  198. (vgadev->decodes & VGA_RSRC_LEGACY_IO))
  199. rsrc |= VGA_RSRC_LEGACY_IO;
  200. if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
  201. (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
  202. rsrc |= VGA_RSRC_LEGACY_MEM;
  203. vgaarb_dbg(dev, "%s: %d\n", __func__, rsrc);
  204. vgaarb_dbg(dev, "%s: owns: %d\n", __func__, vgadev->owns);
  205. /* Check what resources we need to acquire */
  206. wants = rsrc & ~vgadev->owns;
  207. /* We already own everything, just mark locked & bye bye */
  208. if (wants == 0)
  209. goto lock_them;
  210. /* We don't need to request a legacy resource, we just enable
  211. * appropriate decoding and go
  212. */
  213. legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
  214. if (legacy_wants == 0)
  215. goto enable_them;
  216. /* Ok, we don't, let's find out how we need to kick off */
  217. list_for_each_entry(conflict, &vga_list, list) {
  218. unsigned int lwants = legacy_wants;
  219. unsigned int change_bridge = 0;
  220. /* Don't conflict with myself */
  221. if (vgadev == conflict)
  222. continue;
  223. /* We have a possible conflict. before we go further, we must
  224. * check if we sit on the same bus as the conflicting device.
  225. * if we don't, then we must tie both IO and MEM resources
  226. * together since there is only a single bit controlling
  227. * VGA forwarding on P2P bridges
  228. */
  229. if (vgadev->pdev->bus != conflict->pdev->bus) {
  230. change_bridge = 1;
  231. lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  232. }
  233. /* Check if the guy has a lock on the resource. If he does,
  234. * return the conflicting entry
  235. */
  236. if (conflict->locks & lwants)
  237. return conflict;
  238. /* Ok, now check if it owns the resource we want. We can
  239. * lock resources that are not decoded, therefore a device
  240. * can own resources it doesn't decode.
  241. */
  242. match = lwants & conflict->owns;
  243. if (!match)
  244. continue;
  245. /* looks like he doesn't have a lock, we can steal
  246. * them from him
  247. */
  248. flags = 0;
  249. pci_bits = 0;
  250. /* If we can't control legacy resources via the bridge, we
  251. * also need to disable normal decoding.
  252. */
  253. if (!conflict->bridge_has_one_vga) {
  254. if ((match & conflict->decodes) & VGA_RSRC_LEGACY_MEM)
  255. pci_bits |= PCI_COMMAND_MEMORY;
  256. if ((match & conflict->decodes) & VGA_RSRC_LEGACY_IO)
  257. pci_bits |= PCI_COMMAND_IO;
  258. if (pci_bits)
  259. flags |= PCI_VGA_STATE_CHANGE_DECODES;
  260. }
  261. if (change_bridge)
  262. flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
  263. pci_set_vga_state(conflict->pdev, false, pci_bits, flags);
  264. conflict->owns &= ~match;
  265. /* If we disabled normal decoding, reflect it in owns */
  266. if (pci_bits & PCI_COMMAND_MEMORY)
  267. conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
  268. if (pci_bits & PCI_COMMAND_IO)
  269. conflict->owns &= ~VGA_RSRC_NORMAL_IO;
  270. }
  271. enable_them:
  272. /* ok dude, we got it, everybody conflicting has been disabled, let's
  273. * enable us. Mark any bits in "owns" regardless of whether we
  274. * decoded them. We can lock resources we don't decode, therefore
  275. * we must track them via "owns".
  276. */
  277. flags = 0;
  278. pci_bits = 0;
  279. if (!vgadev->bridge_has_one_vga) {
  280. flags |= PCI_VGA_STATE_CHANGE_DECODES;
  281. if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
  282. pci_bits |= PCI_COMMAND_MEMORY;
  283. if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
  284. pci_bits |= PCI_COMMAND_IO;
  285. }
  286. if (wants & VGA_RSRC_LEGACY_MASK)
  287. flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
  288. pci_set_vga_state(vgadev->pdev, true, pci_bits, flags);
  289. vgadev->owns |= wants;
  290. lock_them:
  291. vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
  292. if (rsrc & VGA_RSRC_LEGACY_IO)
  293. vgadev->io_lock_cnt++;
  294. if (rsrc & VGA_RSRC_LEGACY_MEM)
  295. vgadev->mem_lock_cnt++;
  296. if (rsrc & VGA_RSRC_NORMAL_IO)
  297. vgadev->io_norm_cnt++;
  298. if (rsrc & VGA_RSRC_NORMAL_MEM)
  299. vgadev->mem_norm_cnt++;
  300. return NULL;
  301. }
  302. static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
  303. {
  304. struct device *dev = &vgadev->pdev->dev;
  305. unsigned int old_locks = vgadev->locks;
  306. vgaarb_dbg(dev, "%s\n", __func__);
  307. /* Update our counters, and account for equivalent legacy resources
  308. * if we decode them
  309. */
  310. if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
  311. vgadev->io_norm_cnt--;
  312. if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
  313. rsrc |= VGA_RSRC_LEGACY_IO;
  314. }
  315. if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
  316. vgadev->mem_norm_cnt--;
  317. if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
  318. rsrc |= VGA_RSRC_LEGACY_MEM;
  319. }
  320. if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
  321. vgadev->io_lock_cnt--;
  322. if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
  323. vgadev->mem_lock_cnt--;
  324. /* Just clear lock bits, we do lazy operations so we don't really
  325. * have to bother about anything else at this point
  326. */
  327. if (vgadev->io_lock_cnt == 0)
  328. vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
  329. if (vgadev->mem_lock_cnt == 0)
  330. vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
  331. /* Kick the wait queue in case somebody was waiting if we actually
  332. * released something
  333. */
  334. if (old_locks != vgadev->locks)
  335. wake_up_all(&vga_wait_queue);
  336. }
  337. /**
  338. * vga_get - acquire & locks VGA resources
  339. * @pdev: pci device of the VGA card or NULL for the system default
  340. * @rsrc: bit mask of resources to acquire and lock
  341. * @interruptible: blocking should be interruptible by signals ?
  342. *
  343. * This function acquires VGA resources for the given card and mark those
  344. * resources locked. If the resource requested are "normal" (and not legacy)
  345. * resources, the arbiter will first check whether the card is doing legacy
  346. * decoding for that type of resource. If yes, the lock is "converted" into a
  347. * legacy resource lock.
  348. *
  349. * The arbiter will first look for all VGA cards that might conflict and disable
  350. * their IOs and/or Memory access, including VGA forwarding on P2P bridges if
  351. * necessary, so that the requested resources can be used. Then, the card is
  352. * marked as locking these resources and the IO and/or Memory accesses are
  353. * enabled on the card (including VGA forwarding on parent P2P bridges if any).
  354. *
  355. * This function will block if some conflicting card is already locking one of
  356. * the required resources (or any resource on a different bus segment, since P2P
  357. * bridges don't differentiate VGA memory and IO afaik). You can indicate
  358. * whether this blocking should be interruptible by a signal (for userland
  359. * interface) or not.
  360. *
  361. * Must not be called at interrupt time or in atomic context. If the card
  362. * already owns the resources, the function succeeds. Nested calls are
  363. * supported (a per-resource counter is maintained)
  364. *
  365. * On success, release the VGA resource again with vga_put().
  366. *
  367. * Returns:
  368. *
  369. * 0 on success, negative error code on failure.
  370. */
  371. int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
  372. {
  373. struct vga_device *vgadev, *conflict;
  374. unsigned long flags;
  375. wait_queue_entry_t wait;
  376. int rc = 0;
  377. vga_check_first_use();
  378. /* The one who calls us should check for this, but lets be sure... */
  379. if (pdev == NULL)
  380. pdev = vga_default_device();
  381. if (pdev == NULL)
  382. return 0;
  383. for (;;) {
  384. spin_lock_irqsave(&vga_lock, flags);
  385. vgadev = vgadev_find(pdev);
  386. if (vgadev == NULL) {
  387. spin_unlock_irqrestore(&vga_lock, flags);
  388. rc = -ENODEV;
  389. break;
  390. }
  391. conflict = __vga_tryget(vgadev, rsrc);
  392. spin_unlock_irqrestore(&vga_lock, flags);
  393. if (conflict == NULL)
  394. break;
  395. /* We have a conflict, we wait until somebody kicks the
  396. * work queue. Currently we have one work queue that we
  397. * kick each time some resources are released, but it would
  398. * be fairly easy to have a per device one so that we only
  399. * need to attach to the conflicting device
  400. */
  401. init_waitqueue_entry(&wait, current);
  402. add_wait_queue(&vga_wait_queue, &wait);
  403. set_current_state(interruptible ?
  404. TASK_INTERRUPTIBLE :
  405. TASK_UNINTERRUPTIBLE);
  406. if (interruptible && signal_pending(current)) {
  407. __set_current_state(TASK_RUNNING);
  408. remove_wait_queue(&vga_wait_queue, &wait);
  409. rc = -ERESTARTSYS;
  410. break;
  411. }
  412. schedule();
  413. remove_wait_queue(&vga_wait_queue, &wait);
  414. }
  415. return rc;
  416. }
  417. EXPORT_SYMBOL(vga_get);
  418. /**
  419. * vga_tryget - try to acquire & lock legacy VGA resources
  420. * @pdev: pci devivce of VGA card or NULL for system default
  421. * @rsrc: bit mask of resources to acquire and lock
  422. *
  423. * This function performs the same operation as vga_get(), but will return an
  424. * error (-EBUSY) instead of blocking if the resources are already locked by
  425. * another card. It can be called in any context
  426. *
  427. * On success, release the VGA resource again with vga_put().
  428. *
  429. * Returns:
  430. *
  431. * 0 on success, negative error code on failure.
  432. */
  433. static int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
  434. {
  435. struct vga_device *vgadev;
  436. unsigned long flags;
  437. int rc = 0;
  438. vga_check_first_use();
  439. /* The one who calls us should check for this, but lets be sure... */
  440. if (pdev == NULL)
  441. pdev = vga_default_device();
  442. if (pdev == NULL)
  443. return 0;
  444. spin_lock_irqsave(&vga_lock, flags);
  445. vgadev = vgadev_find(pdev);
  446. if (vgadev == NULL) {
  447. rc = -ENODEV;
  448. goto bail;
  449. }
  450. if (__vga_tryget(vgadev, rsrc))
  451. rc = -EBUSY;
  452. bail:
  453. spin_unlock_irqrestore(&vga_lock, flags);
  454. return rc;
  455. }
  456. /**
  457. * vga_put - release lock on legacy VGA resources
  458. * @pdev: pci device of VGA card or NULL for system default
  459. * @rsrc: but mask of resource to release
  460. *
  461. * This fuction releases resources previously locked by vga_get() or
  462. * vga_tryget(). The resources aren't disabled right away, so that a subsequence
  463. * vga_get() on the same card will succeed immediately. Resources have a
  464. * counter, so locks are only released if the counter reaches 0.
  465. */
  466. void vga_put(struct pci_dev *pdev, unsigned int rsrc)
  467. {
  468. struct vga_device *vgadev;
  469. unsigned long flags;
  470. /* The one who calls us should check for this, but lets be sure... */
  471. if (pdev == NULL)
  472. pdev = vga_default_device();
  473. if (pdev == NULL)
  474. return;
  475. spin_lock_irqsave(&vga_lock, flags);
  476. vgadev = vgadev_find(pdev);
  477. if (vgadev == NULL)
  478. goto bail;
  479. __vga_put(vgadev, rsrc);
  480. bail:
  481. spin_unlock_irqrestore(&vga_lock, flags);
  482. }
  483. EXPORT_SYMBOL(vga_put);
  484. static bool vga_is_firmware_default(struct pci_dev *pdev)
  485. {
  486. #if defined(CONFIG_X86) || defined(CONFIG_IA64)
  487. u64 base = screen_info.lfb_base;
  488. u64 size = screen_info.lfb_size;
  489. u64 limit;
  490. resource_size_t start, end;
  491. unsigned long flags;
  492. int i;
  493. /* Select the device owning the boot framebuffer if there is one */
  494. if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
  495. base |= (u64)screen_info.ext_lfb_base << 32;
  496. limit = base + size;
  497. /* Does firmware framebuffer belong to us? */
  498. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  499. flags = pci_resource_flags(pdev, i);
  500. if ((flags & IORESOURCE_MEM) == 0)
  501. continue;
  502. start = pci_resource_start(pdev, i);
  503. end = pci_resource_end(pdev, i);
  504. if (!start || !end)
  505. continue;
  506. if (base < start || limit >= end)
  507. continue;
  508. return true;
  509. }
  510. #endif
  511. return false;
  512. }
  513. static bool vga_arb_integrated_gpu(struct device *dev)
  514. {
  515. #if defined(CONFIG_ACPI)
  516. struct acpi_device *adev = ACPI_COMPANION(dev);
  517. return adev && !strcmp(acpi_device_hid(adev), ACPI_VIDEO_HID);
  518. #else
  519. return false;
  520. #endif
  521. }
  522. /*
  523. * Return true if vgadev is a better default VGA device than the best one
  524. * we've seen so far.
  525. */
  526. static bool vga_is_boot_device(struct vga_device *vgadev)
  527. {
  528. struct vga_device *boot_vga = vgadev_find(vga_default_device());
  529. struct pci_dev *pdev = vgadev->pdev;
  530. u16 cmd, boot_cmd;
  531. /*
  532. * We select the default VGA device in this order:
  533. * Firmware framebuffer (see vga_arb_select_default_device())
  534. * Legacy VGA device (owns VGA_RSRC_LEGACY_MASK)
  535. * Non-legacy integrated device (see vga_arb_select_default_device())
  536. * Non-legacy discrete device (see vga_arb_select_default_device())
  537. * Other device (see vga_arb_select_default_device())
  538. */
  539. /*
  540. * We always prefer a firmware default device, so if we've already
  541. * found one, there's no need to consider vgadev.
  542. */
  543. if (boot_vga && boot_vga->is_firmware_default)
  544. return false;
  545. if (vga_is_firmware_default(pdev)) {
  546. vgadev->is_firmware_default = true;
  547. return true;
  548. }
  549. /*
  550. * A legacy VGA device has MEM and IO enabled and any bridges
  551. * leading to it have PCI_BRIDGE_CTL_VGA enabled so the legacy
  552. * resources ([mem 0xa0000-0xbffff], [io 0x3b0-0x3bb], etc) are
  553. * routed to it.
  554. *
  555. * We use the first one we find, so if we've already found one,
  556. * vgadev is no better.
  557. */
  558. if (boot_vga &&
  559. (boot_vga->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)
  560. return false;
  561. if ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)
  562. return true;
  563. /*
  564. * If we haven't found a legacy VGA device, accept a non-legacy
  565. * device. It may have either IO or MEM enabled, and bridges may
  566. * not have PCI_BRIDGE_CTL_VGA enabled, so it may not be able to
  567. * use legacy VGA resources. Prefer an integrated GPU over others.
  568. */
  569. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  570. if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
  571. /*
  572. * An integrated GPU overrides a previous non-legacy
  573. * device. We expect only a single integrated GPU, but if
  574. * there are more, we use the *last* because that was the
  575. * previous behavior.
  576. */
  577. if (vga_arb_integrated_gpu(&pdev->dev))
  578. return true;
  579. /*
  580. * We prefer the first non-legacy discrete device we find.
  581. * If we already found one, vgadev is no better.
  582. */
  583. if (boot_vga) {
  584. pci_read_config_word(boot_vga->pdev, PCI_COMMAND,
  585. &boot_cmd);
  586. if (boot_cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
  587. return false;
  588. }
  589. return true;
  590. }
  591. /*
  592. * vgadev has neither IO nor MEM enabled. If we haven't found any
  593. * other VGA devices, it is the best candidate so far.
  594. */
  595. if (!boot_vga)
  596. return true;
  597. return false;
  598. }
  599. /*
  600. * Rules for using a bridge to control a VGA descendant decoding: if a bridge
  601. * has only one VGA descendant then it can be used to control the VGA routing
  602. * for that device. It should always use the bridge closest to the device to
  603. * control it. If a bridge has a direct VGA descendant, but also have a sub-
  604. * bridge VGA descendant then we cannot use that bridge to control the direct
  605. * VGA descendant. So for every device we register, we need to iterate all
  606. * its parent bridges so we can invalidate any devices using them properly.
  607. */
  608. static void vga_arbiter_check_bridge_sharing(struct vga_device *vgadev)
  609. {
  610. struct vga_device *same_bridge_vgadev;
  611. struct pci_bus *new_bus, *bus;
  612. struct pci_dev *new_bridge, *bridge;
  613. vgadev->bridge_has_one_vga = true;
  614. if (list_empty(&vga_list)) {
  615. vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n");
  616. return;
  617. }
  618. /* okay iterate the new devices bridge hierarachy */
  619. new_bus = vgadev->pdev->bus;
  620. while (new_bus) {
  621. new_bridge = new_bus->self;
  622. /* go through list of devices already registered */
  623. list_for_each_entry(same_bridge_vgadev, &vga_list, list) {
  624. bus = same_bridge_vgadev->pdev->bus;
  625. bridge = bus->self;
  626. /* see if the share a bridge with this device */
  627. if (new_bridge == bridge) {
  628. /*
  629. * If their direct parent bridge is the same
  630. * as any bridge of this device then it can't
  631. * be used for that device.
  632. */
  633. same_bridge_vgadev->bridge_has_one_vga = false;
  634. }
  635. /*
  636. * Now iterate the previous devices bridge hierarchy.
  637. * If the new devices parent bridge is in the other
  638. * devices hierarchy then we can't use it to control
  639. * this device
  640. */
  641. while (bus) {
  642. bridge = bus->self;
  643. if (bridge && bridge == vgadev->pdev->bus->self)
  644. vgadev->bridge_has_one_vga = false;
  645. bus = bus->parent;
  646. }
  647. }
  648. new_bus = new_bus->parent;
  649. }
  650. if (vgadev->bridge_has_one_vga)
  651. vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n");
  652. else
  653. vgaarb_info(&vgadev->pdev->dev, "no bridge control possible\n");
  654. }
  655. /*
  656. * Currently, we assume that the "initial" setup of the system is
  657. * not sane, that is we come up with conflicting devices and let
  658. * the arbiter's client decides if devices decodes or not legacy
  659. * things.
  660. */
  661. static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
  662. {
  663. struct vga_device *vgadev;
  664. unsigned long flags;
  665. struct pci_bus *bus;
  666. struct pci_dev *bridge;
  667. u16 cmd;
  668. /* Only deal with VGA class devices */
  669. if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  670. return false;
  671. /* Allocate structure */
  672. vgadev = kzalloc(sizeof(struct vga_device), GFP_KERNEL);
  673. if (vgadev == NULL) {
  674. vgaarb_err(&pdev->dev, "failed to allocate VGA arbiter data\n");
  675. /*
  676. * What to do on allocation failure ? For now, let's just do
  677. * nothing, I'm not sure there is anything saner to be done.
  678. */
  679. return false;
  680. }
  681. /* Take lock & check for duplicates */
  682. spin_lock_irqsave(&vga_lock, flags);
  683. if (vgadev_find(pdev) != NULL) {
  684. BUG_ON(1);
  685. goto fail;
  686. }
  687. vgadev->pdev = pdev;
  688. /* By default, assume we decode everything */
  689. vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  690. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  691. /* by default mark it as decoding */
  692. vga_decode_count++;
  693. /* Mark that we "own" resources based on our enables, we will
  694. * clear that below if the bridge isn't forwarding
  695. */
  696. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  697. if (cmd & PCI_COMMAND_IO)
  698. vgadev->owns |= VGA_RSRC_LEGACY_IO;
  699. if (cmd & PCI_COMMAND_MEMORY)
  700. vgadev->owns |= VGA_RSRC_LEGACY_MEM;
  701. /* Check if VGA cycles can get down to us */
  702. bus = pdev->bus;
  703. while (bus) {
  704. bridge = bus->self;
  705. if (bridge) {
  706. u16 l;
  707. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &l);
  708. if (!(l & PCI_BRIDGE_CTL_VGA)) {
  709. vgadev->owns = 0;
  710. break;
  711. }
  712. }
  713. bus = bus->parent;
  714. }
  715. if (vga_is_boot_device(vgadev)) {
  716. vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
  717. vga_default_device() ?
  718. " (overriding previous)" : "");
  719. vga_set_default_device(pdev);
  720. }
  721. vga_arbiter_check_bridge_sharing(vgadev);
  722. /* Add to the list */
  723. list_add_tail(&vgadev->list, &vga_list);
  724. vga_count++;
  725. vgaarb_info(&pdev->dev, "VGA device added: decodes=%s,owns=%s,locks=%s\n",
  726. vga_iostate_to_str(vgadev->decodes),
  727. vga_iostate_to_str(vgadev->owns),
  728. vga_iostate_to_str(vgadev->locks));
  729. spin_unlock_irqrestore(&vga_lock, flags);
  730. return true;
  731. fail:
  732. spin_unlock_irqrestore(&vga_lock, flags);
  733. kfree(vgadev);
  734. return false;
  735. }
  736. static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
  737. {
  738. struct vga_device *vgadev;
  739. unsigned long flags;
  740. bool ret = true;
  741. spin_lock_irqsave(&vga_lock, flags);
  742. vgadev = vgadev_find(pdev);
  743. if (vgadev == NULL) {
  744. ret = false;
  745. goto bail;
  746. }
  747. if (vga_default == pdev)
  748. vga_set_default_device(NULL);
  749. if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
  750. vga_decode_count--;
  751. /* Remove entry from list */
  752. list_del(&vgadev->list);
  753. vga_count--;
  754. /* Wake up all possible waiters */
  755. wake_up_all(&vga_wait_queue);
  756. bail:
  757. spin_unlock_irqrestore(&vga_lock, flags);
  758. kfree(vgadev);
  759. return ret;
  760. }
  761. /* this is called with the lock */
  762. static inline void vga_update_device_decodes(struct vga_device *vgadev,
  763. int new_decodes)
  764. {
  765. struct device *dev = &vgadev->pdev->dev;
  766. int old_decodes, decodes_removed, decodes_unlocked;
  767. old_decodes = vgadev->decodes;
  768. decodes_removed = ~new_decodes & old_decodes;
  769. decodes_unlocked = vgadev->locks & decodes_removed;
  770. vgadev->decodes = new_decodes;
  771. vgaarb_info(dev, "changed VGA decodes: olddecodes=%s,decodes=%s:owns=%s\n",
  772. vga_iostate_to_str(old_decodes),
  773. vga_iostate_to_str(vgadev->decodes),
  774. vga_iostate_to_str(vgadev->owns));
  775. /* if we removed locked decodes, lock count goes to zero, and release */
  776. if (decodes_unlocked) {
  777. if (decodes_unlocked & VGA_RSRC_LEGACY_IO)
  778. vgadev->io_lock_cnt = 0;
  779. if (decodes_unlocked & VGA_RSRC_LEGACY_MEM)
  780. vgadev->mem_lock_cnt = 0;
  781. __vga_put(vgadev, decodes_unlocked);
  782. }
  783. /* change decodes counter */
  784. if (old_decodes & VGA_RSRC_LEGACY_MASK &&
  785. !(new_decodes & VGA_RSRC_LEGACY_MASK))
  786. vga_decode_count--;
  787. if (!(old_decodes & VGA_RSRC_LEGACY_MASK) &&
  788. new_decodes & VGA_RSRC_LEGACY_MASK)
  789. vga_decode_count++;
  790. vgaarb_dbg(dev, "decoding count now is: %d\n", vga_decode_count);
  791. }
  792. static void __vga_set_legacy_decoding(struct pci_dev *pdev,
  793. unsigned int decodes,
  794. bool userspace)
  795. {
  796. struct vga_device *vgadev;
  797. unsigned long flags;
  798. decodes &= VGA_RSRC_LEGACY_MASK;
  799. spin_lock_irqsave(&vga_lock, flags);
  800. vgadev = vgadev_find(pdev);
  801. if (vgadev == NULL)
  802. goto bail;
  803. /* don't let userspace futz with kernel driver decodes */
  804. if (userspace && vgadev->set_decode)
  805. goto bail;
  806. /* update the device decodes + counter */
  807. vga_update_device_decodes(vgadev, decodes);
  808. /* XXX if somebody is going from "doesn't decode" to "decodes" state
  809. * here, additional care must be taken as we may have pending owner
  810. * ship of non-legacy region ...
  811. */
  812. bail:
  813. spin_unlock_irqrestore(&vga_lock, flags);
  814. }
  815. /**
  816. * vga_set_legacy_decoding
  817. * @pdev: pci device of the VGA card
  818. * @decodes: bit mask of what legacy regions the card decodes
  819. *
  820. * Indicates to the arbiter if the card decodes legacy VGA IOs, legacy VGA
  821. * Memory, both, or none. All cards default to both, the card driver (fbdev for
  822. * example) should tell the arbiter if it has disabled legacy decoding, so the
  823. * card can be left out of the arbitration process (and can be safe to take
  824. * interrupts at any time.
  825. */
  826. void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
  827. {
  828. __vga_set_legacy_decoding(pdev, decodes, false);
  829. }
  830. EXPORT_SYMBOL(vga_set_legacy_decoding);
  831. /**
  832. * vga_client_register - register or unregister a VGA arbitration client
  833. * @pdev: pci device of the VGA client
  834. * @set_decode: vga decode change callback
  835. *
  836. * Clients have two callback mechanisms they can use.
  837. *
  838. * @set_decode callback: If a client can disable its GPU VGA resource, it
  839. * will get a callback from this to set the encode/decode state.
  840. *
  841. * Rationale: we cannot disable VGA decode resources unconditionally some single
  842. * GPU laptops seem to require ACPI or BIOS access to the VGA registers to
  843. * control things like backlights etc. Hopefully newer multi-GPU laptops do
  844. * something saner, and desktops won't have any special ACPI for this. The
  845. * driver will get a callback when VGA arbitration is first used by userspace
  846. * since some older X servers have issues.
  847. *
  848. * This function does not check whether a client for @pdev has been registered
  849. * already.
  850. *
  851. * To unregister just call vga_client_unregister().
  852. *
  853. * Returns: 0 on success, -1 on failure
  854. */
  855. int vga_client_register(struct pci_dev *pdev,
  856. unsigned int (*set_decode)(struct pci_dev *pdev, bool decode))
  857. {
  858. int ret = -ENODEV;
  859. struct vga_device *vgadev;
  860. unsigned long flags;
  861. spin_lock_irqsave(&vga_lock, flags);
  862. vgadev = vgadev_find(pdev);
  863. if (!vgadev)
  864. goto bail;
  865. vgadev->set_decode = set_decode;
  866. ret = 0;
  867. bail:
  868. spin_unlock_irqrestore(&vga_lock, flags);
  869. return ret;
  870. }
  871. EXPORT_SYMBOL(vga_client_register);
  872. /*
  873. * Char driver implementation
  874. *
  875. * Semantics is:
  876. *
  877. * open : open user instance of the arbitrer. by default, it's
  878. * attached to the default VGA device of the system.
  879. *
  880. * close : close user instance, release locks
  881. *
  882. * read : return a string indicating the status of the target.
  883. * an IO state string is of the form {io,mem,io+mem,none},
  884. * mc and ic are respectively mem and io lock counts (for
  885. * debugging/diagnostic only). "decodes" indicate what the
  886. * card currently decodes, "owns" indicates what is currently
  887. * enabled on it, and "locks" indicates what is locked by this
  888. * card. If the card is unplugged, we get "invalid" then for
  889. * card_ID and an -ENODEV error is returned for any command
  890. * until a new card is targeted
  891. *
  892. * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
  893. *
  894. * write : write a command to the arbiter. List of commands is:
  895. *
  896. * target <card_ID> : switch target to card <card_ID> (see below)
  897. * lock <io_state> : acquires locks on target ("none" is invalid io_state)
  898. * trylock <io_state> : non-blocking acquire locks on target
  899. * unlock <io_state> : release locks on target
  900. * unlock all : release all locks on target held by this user
  901. * decodes <io_state> : set the legacy decoding attributes for the card
  902. *
  903. * poll : event if something change on any card (not just the target)
  904. *
  905. * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
  906. * to go back to the system default card (TODO: not implemented yet).
  907. * Currently, only PCI is supported as a prefix, but the userland API may
  908. * support other bus types in the future, even if the current kernel
  909. * implementation doesn't.
  910. *
  911. * Note about locks:
  912. *
  913. * The driver keeps track of which user has what locks on which card. It
  914. * supports stacking, like the kernel one. This complexifies the implementation
  915. * a bit, but makes the arbiter more tolerant to userspace problems and able
  916. * to properly cleanup in all cases when a process dies.
  917. * Currently, a max of 16 cards simultaneously can have locks issued from
  918. * userspace for a given user (file descriptor instance) of the arbiter.
  919. *
  920. * If the device is hot-unplugged, there is a hook inside the module to notify
  921. * they being added/removed in the system and automatically added/removed in
  922. * the arbiter.
  923. */
  924. #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
  925. #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
  926. /*
  927. * Each user has an array of these, tracking which cards have locks
  928. */
  929. struct vga_arb_user_card {
  930. struct pci_dev *pdev;
  931. unsigned int mem_cnt;
  932. unsigned int io_cnt;
  933. };
  934. struct vga_arb_private {
  935. struct list_head list;
  936. struct pci_dev *target;
  937. struct vga_arb_user_card cards[MAX_USER_CARDS];
  938. spinlock_t lock;
  939. };
  940. static LIST_HEAD(vga_user_list);
  941. static DEFINE_SPINLOCK(vga_user_lock);
  942. /*
  943. * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
  944. * returns the respective values. If the string is not in this format,
  945. * it returns 0.
  946. */
  947. static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
  948. unsigned int *bus, unsigned int *devfn)
  949. {
  950. int n;
  951. unsigned int slot, func;
  952. n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
  953. if (n != 4)
  954. return 0;
  955. *devfn = PCI_DEVFN(slot, func);
  956. return 1;
  957. }
  958. static ssize_t vga_arb_read(struct file *file, char __user *buf,
  959. size_t count, loff_t *ppos)
  960. {
  961. struct vga_arb_private *priv = file->private_data;
  962. struct vga_device *vgadev;
  963. struct pci_dev *pdev;
  964. unsigned long flags;
  965. size_t len;
  966. int rc;
  967. char *lbuf;
  968. lbuf = kmalloc(1024, GFP_KERNEL);
  969. if (lbuf == NULL)
  970. return -ENOMEM;
  971. /* Protects vga_list */
  972. spin_lock_irqsave(&vga_lock, flags);
  973. /* If we are targeting the default, use it */
  974. pdev = priv->target;
  975. if (pdev == NULL || pdev == PCI_INVALID_CARD) {
  976. spin_unlock_irqrestore(&vga_lock, flags);
  977. len = sprintf(lbuf, "invalid");
  978. goto done;
  979. }
  980. /* Find card vgadev structure */
  981. vgadev = vgadev_find(pdev);
  982. if (vgadev == NULL) {
  983. /* Wow, it's not in the list, that shouldn't happen,
  984. * let's fix us up and return invalid card
  985. */
  986. spin_unlock_irqrestore(&vga_lock, flags);
  987. len = sprintf(lbuf, "invalid");
  988. goto done;
  989. }
  990. /* Fill the buffer with infos */
  991. len = snprintf(lbuf, 1024,
  992. "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%u:%u)\n",
  993. vga_decode_count, pci_name(pdev),
  994. vga_iostate_to_str(vgadev->decodes),
  995. vga_iostate_to_str(vgadev->owns),
  996. vga_iostate_to_str(vgadev->locks),
  997. vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
  998. spin_unlock_irqrestore(&vga_lock, flags);
  999. done:
  1000. /* Copy that to user */
  1001. if (len > count)
  1002. len = count;
  1003. rc = copy_to_user(buf, lbuf, len);
  1004. kfree(lbuf);
  1005. if (rc)
  1006. return -EFAULT;
  1007. return len;
  1008. }
  1009. /*
  1010. * TODO: To avoid parsing inside kernel and to improve the speed we may
  1011. * consider use ioctl here
  1012. */
  1013. static ssize_t vga_arb_write(struct file *file, const char __user *buf,
  1014. size_t count, loff_t *ppos)
  1015. {
  1016. struct vga_arb_private *priv = file->private_data;
  1017. struct vga_arb_user_card *uc = NULL;
  1018. struct pci_dev *pdev;
  1019. unsigned int io_state;
  1020. char kbuf[64], *curr_pos;
  1021. size_t remaining = count;
  1022. int ret_val;
  1023. int i;
  1024. if (count >= sizeof(kbuf))
  1025. return -EINVAL;
  1026. if (copy_from_user(kbuf, buf, count))
  1027. return -EFAULT;
  1028. curr_pos = kbuf;
  1029. kbuf[count] = '\0'; /* Just to make sure... */
  1030. if (strncmp(curr_pos, "lock ", 5) == 0) {
  1031. curr_pos += 5;
  1032. remaining -= 5;
  1033. pr_debug("client 0x%p called 'lock'\n", priv);
  1034. if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
  1035. ret_val = -EPROTO;
  1036. goto done;
  1037. }
  1038. if (io_state == VGA_RSRC_NONE) {
  1039. ret_val = -EPROTO;
  1040. goto done;
  1041. }
  1042. pdev = priv->target;
  1043. if (priv->target == NULL) {
  1044. ret_val = -ENODEV;
  1045. goto done;
  1046. }
  1047. vga_get_uninterruptible(pdev, io_state);
  1048. /* Update the client's locks lists... */
  1049. for (i = 0; i < MAX_USER_CARDS; i++) {
  1050. if (priv->cards[i].pdev == pdev) {
  1051. if (io_state & VGA_RSRC_LEGACY_IO)
  1052. priv->cards[i].io_cnt++;
  1053. if (io_state & VGA_RSRC_LEGACY_MEM)
  1054. priv->cards[i].mem_cnt++;
  1055. break;
  1056. }
  1057. }
  1058. ret_val = count;
  1059. goto done;
  1060. } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
  1061. curr_pos += 7;
  1062. remaining -= 7;
  1063. pr_debug("client 0x%p called 'unlock'\n", priv);
  1064. if (strncmp(curr_pos, "all", 3) == 0)
  1065. io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  1066. else {
  1067. if (!vga_str_to_iostate
  1068. (curr_pos, remaining, &io_state)) {
  1069. ret_val = -EPROTO;
  1070. goto done;
  1071. }
  1072. /* TODO: Add this?
  1073. if (io_state == VGA_RSRC_NONE) {
  1074. ret_val = -EPROTO;
  1075. goto done;
  1076. }
  1077. */
  1078. }
  1079. pdev = priv->target;
  1080. if (priv->target == NULL) {
  1081. ret_val = -ENODEV;
  1082. goto done;
  1083. }
  1084. for (i = 0; i < MAX_USER_CARDS; i++) {
  1085. if (priv->cards[i].pdev == pdev)
  1086. uc = &priv->cards[i];
  1087. }
  1088. if (!uc) {
  1089. ret_val = -EINVAL;
  1090. goto done;
  1091. }
  1092. if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
  1093. ret_val = -EINVAL;
  1094. goto done;
  1095. }
  1096. if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
  1097. ret_val = -EINVAL;
  1098. goto done;
  1099. }
  1100. vga_put(pdev, io_state);
  1101. if (io_state & VGA_RSRC_LEGACY_IO)
  1102. uc->io_cnt--;
  1103. if (io_state & VGA_RSRC_LEGACY_MEM)
  1104. uc->mem_cnt--;
  1105. ret_val = count;
  1106. goto done;
  1107. } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
  1108. curr_pos += 8;
  1109. remaining -= 8;
  1110. pr_debug("client 0x%p called 'trylock'\n", priv);
  1111. if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
  1112. ret_val = -EPROTO;
  1113. goto done;
  1114. }
  1115. /* TODO: Add this?
  1116. if (io_state == VGA_RSRC_NONE) {
  1117. ret_val = -EPROTO;
  1118. goto done;
  1119. }
  1120. */
  1121. pdev = priv->target;
  1122. if (priv->target == NULL) {
  1123. ret_val = -ENODEV;
  1124. goto done;
  1125. }
  1126. if (vga_tryget(pdev, io_state)) {
  1127. /* Update the client's locks lists... */
  1128. for (i = 0; i < MAX_USER_CARDS; i++) {
  1129. if (priv->cards[i].pdev == pdev) {
  1130. if (io_state & VGA_RSRC_LEGACY_IO)
  1131. priv->cards[i].io_cnt++;
  1132. if (io_state & VGA_RSRC_LEGACY_MEM)
  1133. priv->cards[i].mem_cnt++;
  1134. break;
  1135. }
  1136. }
  1137. ret_val = count;
  1138. goto done;
  1139. } else {
  1140. ret_val = -EBUSY;
  1141. goto done;
  1142. }
  1143. } else if (strncmp(curr_pos, "target ", 7) == 0) {
  1144. unsigned int domain, bus, devfn;
  1145. struct vga_device *vgadev;
  1146. curr_pos += 7;
  1147. remaining -= 7;
  1148. pr_debug("client 0x%p called 'target'\n", priv);
  1149. /* if target is default */
  1150. if (!strncmp(curr_pos, "default", 7))
  1151. pdev = pci_dev_get(vga_default_device());
  1152. else {
  1153. if (!vga_pci_str_to_vars(curr_pos, remaining,
  1154. &domain, &bus, &devfn)) {
  1155. ret_val = -EPROTO;
  1156. goto done;
  1157. }
  1158. pdev = pci_get_domain_bus_and_slot(domain, bus, devfn);
  1159. if (!pdev) {
  1160. pr_debug("invalid PCI address %04x:%02x:%02x.%x\n",
  1161. domain, bus, PCI_SLOT(devfn),
  1162. PCI_FUNC(devfn));
  1163. ret_val = -ENODEV;
  1164. goto done;
  1165. }
  1166. pr_debug("%s ==> %04x:%02x:%02x.%x pdev %p\n", curr_pos,
  1167. domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
  1168. pdev);
  1169. }
  1170. vgadev = vgadev_find(pdev);
  1171. pr_debug("vgadev %p\n", vgadev);
  1172. if (vgadev == NULL) {
  1173. if (pdev) {
  1174. vgaarb_dbg(&pdev->dev, "not a VGA device\n");
  1175. pci_dev_put(pdev);
  1176. }
  1177. ret_val = -ENODEV;
  1178. goto done;
  1179. }
  1180. priv->target = pdev;
  1181. for (i = 0; i < MAX_USER_CARDS; i++) {
  1182. if (priv->cards[i].pdev == pdev)
  1183. break;
  1184. if (priv->cards[i].pdev == NULL) {
  1185. priv->cards[i].pdev = pdev;
  1186. priv->cards[i].io_cnt = 0;
  1187. priv->cards[i].mem_cnt = 0;
  1188. break;
  1189. }
  1190. }
  1191. if (i == MAX_USER_CARDS) {
  1192. vgaarb_dbg(&pdev->dev, "maximum user cards (%d) number reached, ignoring this one!\n",
  1193. MAX_USER_CARDS);
  1194. pci_dev_put(pdev);
  1195. /* XXX: which value to return? */
  1196. ret_val = -ENOMEM;
  1197. goto done;
  1198. }
  1199. ret_val = count;
  1200. pci_dev_put(pdev);
  1201. goto done;
  1202. } else if (strncmp(curr_pos, "decodes ", 8) == 0) {
  1203. curr_pos += 8;
  1204. remaining -= 8;
  1205. pr_debug("client 0x%p called 'decodes'\n", priv);
  1206. if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
  1207. ret_val = -EPROTO;
  1208. goto done;
  1209. }
  1210. pdev = priv->target;
  1211. if (priv->target == NULL) {
  1212. ret_val = -ENODEV;
  1213. goto done;
  1214. }
  1215. __vga_set_legacy_decoding(pdev, io_state, true);
  1216. ret_val = count;
  1217. goto done;
  1218. }
  1219. /* If we got here, the message written is not part of the protocol! */
  1220. return -EPROTO;
  1221. done:
  1222. return ret_val;
  1223. }
  1224. static __poll_t vga_arb_fpoll(struct file *file, poll_table *wait)
  1225. {
  1226. pr_debug("%s\n", __func__);
  1227. poll_wait(file, &vga_wait_queue, wait);
  1228. return EPOLLIN;
  1229. }
  1230. static int vga_arb_open(struct inode *inode, struct file *file)
  1231. {
  1232. struct vga_arb_private *priv;
  1233. unsigned long flags;
  1234. pr_debug("%s\n", __func__);
  1235. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1236. if (priv == NULL)
  1237. return -ENOMEM;
  1238. spin_lock_init(&priv->lock);
  1239. file->private_data = priv;
  1240. spin_lock_irqsave(&vga_user_lock, flags);
  1241. list_add(&priv->list, &vga_user_list);
  1242. spin_unlock_irqrestore(&vga_user_lock, flags);
  1243. /* Set the client' lists of locks */
  1244. priv->target = vga_default_device(); /* Maybe this is still null! */
  1245. priv->cards[0].pdev = priv->target;
  1246. priv->cards[0].io_cnt = 0;
  1247. priv->cards[0].mem_cnt = 0;
  1248. return 0;
  1249. }
  1250. static int vga_arb_release(struct inode *inode, struct file *file)
  1251. {
  1252. struct vga_arb_private *priv = file->private_data;
  1253. struct vga_arb_user_card *uc;
  1254. unsigned long flags;
  1255. int i;
  1256. pr_debug("%s\n", __func__);
  1257. spin_lock_irqsave(&vga_user_lock, flags);
  1258. list_del(&priv->list);
  1259. for (i = 0; i < MAX_USER_CARDS; i++) {
  1260. uc = &priv->cards[i];
  1261. if (uc->pdev == NULL)
  1262. continue;
  1263. vgaarb_dbg(&uc->pdev->dev, "uc->io_cnt == %d, uc->mem_cnt == %d\n",
  1264. uc->io_cnt, uc->mem_cnt);
  1265. while (uc->io_cnt--)
  1266. vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
  1267. while (uc->mem_cnt--)
  1268. vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
  1269. }
  1270. spin_unlock_irqrestore(&vga_user_lock, flags);
  1271. kfree(priv);
  1272. return 0;
  1273. }
  1274. /*
  1275. * callback any registered clients to let them know we have a
  1276. * change in VGA cards
  1277. */
  1278. static void vga_arbiter_notify_clients(void)
  1279. {
  1280. struct vga_device *vgadev;
  1281. unsigned long flags;
  1282. uint32_t new_decodes;
  1283. bool new_state;
  1284. if (!vga_arbiter_used)
  1285. return;
  1286. spin_lock_irqsave(&vga_lock, flags);
  1287. list_for_each_entry(vgadev, &vga_list, list) {
  1288. if (vga_count > 1)
  1289. new_state = false;
  1290. else
  1291. new_state = true;
  1292. if (vgadev->set_decode) {
  1293. new_decodes = vgadev->set_decode(vgadev->pdev,
  1294. new_state);
  1295. vga_update_device_decodes(vgadev, new_decodes);
  1296. }
  1297. }
  1298. spin_unlock_irqrestore(&vga_lock, flags);
  1299. }
  1300. static int pci_notify(struct notifier_block *nb, unsigned long action,
  1301. void *data)
  1302. {
  1303. struct device *dev = data;
  1304. struct pci_dev *pdev = to_pci_dev(dev);
  1305. bool notify = false;
  1306. vgaarb_dbg(dev, "%s\n", __func__);
  1307. /* For now we're only intereted in devices added and removed. I didn't
  1308. * test this thing here, so someone needs to double check for the
  1309. * cases of hotplugable vga cards. */
  1310. if (action == BUS_NOTIFY_ADD_DEVICE)
  1311. notify = vga_arbiter_add_pci_device(pdev);
  1312. else if (action == BUS_NOTIFY_DEL_DEVICE)
  1313. notify = vga_arbiter_del_pci_device(pdev);
  1314. if (notify)
  1315. vga_arbiter_notify_clients();
  1316. return 0;
  1317. }
  1318. static struct notifier_block pci_notifier = {
  1319. .notifier_call = pci_notify,
  1320. };
  1321. static const struct file_operations vga_arb_device_fops = {
  1322. .read = vga_arb_read,
  1323. .write = vga_arb_write,
  1324. .poll = vga_arb_fpoll,
  1325. .open = vga_arb_open,
  1326. .release = vga_arb_release,
  1327. .llseek = noop_llseek,
  1328. };
  1329. static struct miscdevice vga_arb_device = {
  1330. MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
  1331. };
  1332. static int __init vga_arb_device_init(void)
  1333. {
  1334. int rc;
  1335. struct pci_dev *pdev;
  1336. rc = misc_register(&vga_arb_device);
  1337. if (rc < 0)
  1338. pr_err("error %d registering device\n", rc);
  1339. bus_register_notifier(&pci_bus_type, &pci_notifier);
  1340. /* We add all PCI devices satisfying VGA class in the arbiter by
  1341. * default */
  1342. pdev = NULL;
  1343. while ((pdev =
  1344. pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  1345. PCI_ANY_ID, pdev)) != NULL)
  1346. vga_arbiter_add_pci_device(pdev);
  1347. pr_info("loaded\n");
  1348. return rc;
  1349. }
  1350. subsys_initcall_sync(vga_arb_device_init);