vgaarb.c 40 KB

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