vgaarb.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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. /* Returns the default VGA device (vgacon's babe) */
  120. struct pci_dev *vga_default_device(void)
  121. {
  122. return vga_default;
  123. }
  124. EXPORT_SYMBOL_GPL(vga_default_device);
  125. void vga_set_default_device(struct pci_dev *pdev)
  126. {
  127. if (vga_default == pdev)
  128. return;
  129. pci_dev_put(vga_default);
  130. vga_default = pci_dev_get(pdev);
  131. }
  132. static inline void vga_irq_set_state(struct vga_device *vgadev, bool state)
  133. {
  134. if (vgadev->irq_set_state)
  135. vgadev->irq_set_state(vgadev->cookie, state);
  136. }
  137. /* If we don't ever use VGA arb we should avoid
  138. turning off anything anywhere due to old X servers getting
  139. confused about the boot device not being VGA */
  140. static void vga_check_first_use(void)
  141. {
  142. /* we should inform all GPUs in the system that
  143. * VGA arb has occurred and to try and disable resources
  144. * if they can */
  145. if (!vga_arbiter_used) {
  146. vga_arbiter_used = true;
  147. vga_arbiter_notify_clients();
  148. }
  149. }
  150. static struct vga_device *__vga_tryget(struct vga_device *vgadev,
  151. unsigned int rsrc)
  152. {
  153. unsigned int wants, legacy_wants, match;
  154. struct vga_device *conflict;
  155. unsigned int pci_bits;
  156. u32 flags = 0;
  157. /* Account for "normal" resources to lock. If we decode the legacy,
  158. * counterpart, we need to request it as well
  159. */
  160. if ((rsrc & VGA_RSRC_NORMAL_IO) &&
  161. (vgadev->decodes & VGA_RSRC_LEGACY_IO))
  162. rsrc |= VGA_RSRC_LEGACY_IO;
  163. if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
  164. (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
  165. rsrc |= VGA_RSRC_LEGACY_MEM;
  166. pr_debug("%s: %d\n", __func__, rsrc);
  167. pr_debug("%s: owns: %d\n", __func__, vgadev->owns);
  168. /* Check what resources we need to acquire */
  169. wants = rsrc & ~vgadev->owns;
  170. /* We already own everything, just mark locked & bye bye */
  171. if (wants == 0)
  172. goto lock_them;
  173. /* We don't need to request a legacy resource, we just enable
  174. * appropriate decoding and go
  175. */
  176. legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
  177. if (legacy_wants == 0)
  178. goto enable_them;
  179. /* Ok, we don't, let's find out how we need to kick off */
  180. list_for_each_entry(conflict, &vga_list, list) {
  181. unsigned int lwants = legacy_wants;
  182. unsigned int change_bridge = 0;
  183. /* Don't conflict with myself */
  184. if (vgadev == conflict)
  185. continue;
  186. /* Check if the architecture allows a conflict between those
  187. * 2 devices or if they are on separate domains
  188. */
  189. if (!vga_conflicts(vgadev->pdev, conflict->pdev))
  190. continue;
  191. /* We have a possible conflict. before we go further, we must
  192. * check if we sit on the same bus as the conflicting device.
  193. * if we don't, then we must tie both IO and MEM resources
  194. * together since there is only a single bit controlling
  195. * VGA forwarding on P2P bridges
  196. */
  197. if (vgadev->pdev->bus != conflict->pdev->bus) {
  198. change_bridge = 1;
  199. lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  200. }
  201. /* Check if the guy has a lock on the resource. If he does,
  202. * return the conflicting entry
  203. */
  204. if (conflict->locks & lwants)
  205. return conflict;
  206. /* Ok, now check if it owns the resource we want. We can
  207. * lock resources that are not decoded, therefore a device
  208. * can own resources it doesn't decode.
  209. */
  210. match = lwants & conflict->owns;
  211. if (!match)
  212. continue;
  213. /* looks like he doesn't have a lock, we can steal
  214. * them from him
  215. */
  216. flags = 0;
  217. pci_bits = 0;
  218. /* If we can't control legacy resources via the bridge, we
  219. * also need to disable normal decoding.
  220. */
  221. if (!conflict->bridge_has_one_vga) {
  222. if ((match & conflict->decodes) & VGA_RSRC_LEGACY_MEM)
  223. pci_bits |= PCI_COMMAND_MEMORY;
  224. if ((match & conflict->decodes) & VGA_RSRC_LEGACY_IO)
  225. pci_bits |= PCI_COMMAND_IO;
  226. if (pci_bits) {
  227. vga_irq_set_state(conflict, false);
  228. flags |= PCI_VGA_STATE_CHANGE_DECODES;
  229. }
  230. }
  231. if (change_bridge)
  232. flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
  233. pci_set_vga_state(conflict->pdev, false, pci_bits, flags);
  234. conflict->owns &= ~match;
  235. /* If we disabled normal decoding, reflect it in owns */
  236. if (pci_bits & PCI_COMMAND_MEMORY)
  237. conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
  238. if (pci_bits & PCI_COMMAND_IO)
  239. conflict->owns &= ~VGA_RSRC_NORMAL_IO;
  240. }
  241. enable_them:
  242. /* ok dude, we got it, everybody conflicting has been disabled, let's
  243. * enable us. Mark any bits in "owns" regardless of whether we
  244. * decoded them. We can lock resources we don't decode, therefore
  245. * we must track them via "owns".
  246. */
  247. flags = 0;
  248. pci_bits = 0;
  249. if (!vgadev->bridge_has_one_vga) {
  250. flags |= PCI_VGA_STATE_CHANGE_DECODES;
  251. if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
  252. pci_bits |= PCI_COMMAND_MEMORY;
  253. if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
  254. pci_bits |= PCI_COMMAND_IO;
  255. }
  256. if (wants & VGA_RSRC_LEGACY_MASK)
  257. flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
  258. pci_set_vga_state(vgadev->pdev, true, pci_bits, flags);
  259. if (!vgadev->bridge_has_one_vga)
  260. vga_irq_set_state(vgadev, true);
  261. vgadev->owns |= wants;
  262. lock_them:
  263. vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
  264. if (rsrc & VGA_RSRC_LEGACY_IO)
  265. vgadev->io_lock_cnt++;
  266. if (rsrc & VGA_RSRC_LEGACY_MEM)
  267. vgadev->mem_lock_cnt++;
  268. if (rsrc & VGA_RSRC_NORMAL_IO)
  269. vgadev->io_norm_cnt++;
  270. if (rsrc & VGA_RSRC_NORMAL_MEM)
  271. vgadev->mem_norm_cnt++;
  272. return NULL;
  273. }
  274. static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
  275. {
  276. unsigned int old_locks = vgadev->locks;
  277. pr_debug("%s\n", __func__);
  278. /* Update our counters, and account for equivalent legacy resources
  279. * if we decode them
  280. */
  281. if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
  282. vgadev->io_norm_cnt--;
  283. if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
  284. rsrc |= VGA_RSRC_LEGACY_IO;
  285. }
  286. if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
  287. vgadev->mem_norm_cnt--;
  288. if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
  289. rsrc |= VGA_RSRC_LEGACY_MEM;
  290. }
  291. if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
  292. vgadev->io_lock_cnt--;
  293. if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
  294. vgadev->mem_lock_cnt--;
  295. /* Just clear lock bits, we do lazy operations so we don't really
  296. * have to bother about anything else at this point
  297. */
  298. if (vgadev->io_lock_cnt == 0)
  299. vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
  300. if (vgadev->mem_lock_cnt == 0)
  301. vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
  302. /* Kick the wait queue in case somebody was waiting if we actually
  303. * released something
  304. */
  305. if (old_locks != vgadev->locks)
  306. wake_up_all(&vga_wait_queue);
  307. }
  308. int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
  309. {
  310. struct vga_device *vgadev, *conflict;
  311. unsigned long flags;
  312. wait_queue_t wait;
  313. int rc = 0;
  314. vga_check_first_use();
  315. /* The one who calls us should check for this, but lets be sure... */
  316. if (pdev == NULL)
  317. pdev = vga_default_device();
  318. if (pdev == NULL)
  319. return 0;
  320. for (;;) {
  321. spin_lock_irqsave(&vga_lock, flags);
  322. vgadev = vgadev_find(pdev);
  323. if (vgadev == NULL) {
  324. spin_unlock_irqrestore(&vga_lock, flags);
  325. rc = -ENODEV;
  326. break;
  327. }
  328. conflict = __vga_tryget(vgadev, rsrc);
  329. spin_unlock_irqrestore(&vga_lock, flags);
  330. if (conflict == NULL)
  331. break;
  332. /* We have a conflict, we wait until somebody kicks the
  333. * work queue. Currently we have one work queue that we
  334. * kick each time some resources are released, but it would
  335. * be fairly easy to have a per device one so that we only
  336. * need to attach to the conflicting device
  337. */
  338. init_waitqueue_entry(&wait, current);
  339. add_wait_queue(&vga_wait_queue, &wait);
  340. set_current_state(interruptible ?
  341. TASK_INTERRUPTIBLE :
  342. TASK_UNINTERRUPTIBLE);
  343. if (interruptible && signal_pending(current)) {
  344. __set_current_state(TASK_RUNNING);
  345. remove_wait_queue(&vga_wait_queue, &wait);
  346. rc = -ERESTARTSYS;
  347. break;
  348. }
  349. schedule();
  350. remove_wait_queue(&vga_wait_queue, &wait);
  351. }
  352. return rc;
  353. }
  354. EXPORT_SYMBOL(vga_get);
  355. int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
  356. {
  357. struct vga_device *vgadev;
  358. unsigned long flags;
  359. int rc = 0;
  360. vga_check_first_use();
  361. /* The one who calls us should check for this, but lets be sure... */
  362. if (pdev == NULL)
  363. pdev = vga_default_device();
  364. if (pdev == NULL)
  365. return 0;
  366. spin_lock_irqsave(&vga_lock, flags);
  367. vgadev = vgadev_find(pdev);
  368. if (vgadev == NULL) {
  369. rc = -ENODEV;
  370. goto bail;
  371. }
  372. if (__vga_tryget(vgadev, rsrc))
  373. rc = -EBUSY;
  374. bail:
  375. spin_unlock_irqrestore(&vga_lock, flags);
  376. return rc;
  377. }
  378. EXPORT_SYMBOL(vga_tryget);
  379. void vga_put(struct pci_dev *pdev, unsigned int rsrc)
  380. {
  381. struct vga_device *vgadev;
  382. unsigned long flags;
  383. /* The one who calls us should check for this, but lets be sure... */
  384. if (pdev == NULL)
  385. pdev = vga_default_device();
  386. if (pdev == NULL)
  387. return;
  388. spin_lock_irqsave(&vga_lock, flags);
  389. vgadev = vgadev_find(pdev);
  390. if (vgadev == NULL)
  391. goto bail;
  392. __vga_put(vgadev, rsrc);
  393. bail:
  394. spin_unlock_irqrestore(&vga_lock, flags);
  395. }
  396. EXPORT_SYMBOL(vga_put);
  397. /*
  398. * Rules for using a bridge to control a VGA descendant decoding: if a bridge
  399. * has only one VGA descendant then it can be used to control the VGA routing
  400. * for that device. It should always use the bridge closest to the device to
  401. * control it. If a bridge has a direct VGA descendant, but also have a sub-
  402. * bridge VGA descendant then we cannot use that bridge to control the direct
  403. * VGA descendant. So for every device we register, we need to iterate all
  404. * its parent bridges so we can invalidate any devices using them properly.
  405. */
  406. static void vga_arbiter_check_bridge_sharing(struct vga_device *vgadev)
  407. {
  408. struct vga_device *same_bridge_vgadev;
  409. struct pci_bus *new_bus, *bus;
  410. struct pci_dev *new_bridge, *bridge;
  411. vgadev->bridge_has_one_vga = true;
  412. if (list_empty(&vga_list))
  413. return;
  414. /* okay iterate the new devices bridge hierarachy */
  415. new_bus = vgadev->pdev->bus;
  416. while (new_bus) {
  417. new_bridge = new_bus->self;
  418. /* go through list of devices already registered */
  419. list_for_each_entry(same_bridge_vgadev, &vga_list, list) {
  420. bus = same_bridge_vgadev->pdev->bus;
  421. bridge = bus->self;
  422. /* see if the share a bridge with this device */
  423. if (new_bridge == bridge) {
  424. /*
  425. * If their direct parent bridge is the same
  426. * as any bridge of this device then it can't
  427. * be used for that device.
  428. */
  429. same_bridge_vgadev->bridge_has_one_vga = false;
  430. }
  431. /*
  432. * Now iterate the previous devices bridge hierarchy.
  433. * If the new devices parent bridge is in the other
  434. * devices hierarchy then we can't use it to control
  435. * this device
  436. */
  437. while (bus) {
  438. bridge = bus->self;
  439. if (bridge && bridge == vgadev->pdev->bus->self)
  440. vgadev->bridge_has_one_vga = false;
  441. bus = bus->parent;
  442. }
  443. }
  444. new_bus = new_bus->parent;
  445. }
  446. }
  447. /*
  448. * Currently, we assume that the "initial" setup of the system is
  449. * not sane, that is we come up with conflicting devices and let
  450. * the arbiter's client decides if devices decodes or not legacy
  451. * things.
  452. */
  453. static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
  454. {
  455. struct vga_device *vgadev;
  456. unsigned long flags;
  457. struct pci_bus *bus;
  458. struct pci_dev *bridge;
  459. u16 cmd;
  460. /* Only deal with VGA class devices */
  461. if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  462. return false;
  463. /* Allocate structure */
  464. vgadev = kzalloc(sizeof(struct vga_device), GFP_KERNEL);
  465. if (vgadev == NULL) {
  466. pr_err("failed to allocate pci device\n");
  467. /*
  468. * What to do on allocation failure ? For now, let's just do
  469. * nothing, I'm not sure there is anything saner to be done.
  470. */
  471. return false;
  472. }
  473. /* Take lock & check for duplicates */
  474. spin_lock_irqsave(&vga_lock, flags);
  475. if (vgadev_find(pdev) != NULL) {
  476. BUG_ON(1);
  477. goto fail;
  478. }
  479. vgadev->pdev = pdev;
  480. /* By default, assume we decode everything */
  481. vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  482. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  483. /* by default mark it as decoding */
  484. vga_decode_count++;
  485. /* Mark that we "own" resources based on our enables, we will
  486. * clear that below if the bridge isn't forwarding
  487. */
  488. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  489. if (cmd & PCI_COMMAND_IO)
  490. vgadev->owns |= VGA_RSRC_LEGACY_IO;
  491. if (cmd & PCI_COMMAND_MEMORY)
  492. vgadev->owns |= VGA_RSRC_LEGACY_MEM;
  493. /* Check if VGA cycles can get down to us */
  494. bus = pdev->bus;
  495. while (bus) {
  496. bridge = bus->self;
  497. if (bridge) {
  498. u16 l;
  499. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &l);
  500. if (!(l & PCI_BRIDGE_CTL_VGA)) {
  501. vgadev->owns = 0;
  502. break;
  503. }
  504. }
  505. bus = bus->parent;
  506. }
  507. /* Deal with VGA default device. Use first enabled one
  508. * by default if arch doesn't have it's own hook
  509. */
  510. if (vga_default == NULL &&
  511. ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)) {
  512. pr_info("setting as boot device: PCI:%s\n", pci_name(pdev));
  513. vga_set_default_device(pdev);
  514. }
  515. vga_arbiter_check_bridge_sharing(vgadev);
  516. /* Add to the list */
  517. list_add(&vgadev->list, &vga_list);
  518. vga_count++;
  519. pr_info("device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
  520. pci_name(pdev),
  521. vga_iostate_to_str(vgadev->decodes),
  522. vga_iostate_to_str(vgadev->owns),
  523. vga_iostate_to_str(vgadev->locks));
  524. spin_unlock_irqrestore(&vga_lock, flags);
  525. return true;
  526. fail:
  527. spin_unlock_irqrestore(&vga_lock, flags);
  528. kfree(vgadev);
  529. return false;
  530. }
  531. static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
  532. {
  533. struct vga_device *vgadev;
  534. unsigned long flags;
  535. bool ret = true;
  536. spin_lock_irqsave(&vga_lock, flags);
  537. vgadev = vgadev_find(pdev);
  538. if (vgadev == NULL) {
  539. ret = false;
  540. goto bail;
  541. }
  542. if (vga_default == pdev)
  543. vga_set_default_device(NULL);
  544. if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
  545. vga_decode_count--;
  546. /* Remove entry from list */
  547. list_del(&vgadev->list);
  548. vga_count--;
  549. /* Notify userland driver that the device is gone so it discards
  550. * it's copies of the pci_dev pointer
  551. */
  552. vga_arb_device_card_gone(pdev);
  553. /* Wake up all possible waiters */
  554. wake_up_all(&vga_wait_queue);
  555. bail:
  556. spin_unlock_irqrestore(&vga_lock, flags);
  557. kfree(vgadev);
  558. return ret;
  559. }
  560. /* this is called with the lock */
  561. static inline void vga_update_device_decodes(struct vga_device *vgadev,
  562. int new_decodes)
  563. {
  564. int old_decodes, decodes_removed, decodes_unlocked;
  565. old_decodes = vgadev->decodes;
  566. decodes_removed = ~new_decodes & old_decodes;
  567. decodes_unlocked = vgadev->locks & decodes_removed;
  568. vgadev->decodes = new_decodes;
  569. pr_info("device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
  570. pci_name(vgadev->pdev),
  571. vga_iostate_to_str(old_decodes),
  572. vga_iostate_to_str(vgadev->decodes),
  573. vga_iostate_to_str(vgadev->owns));
  574. /* if we removed locked decodes, lock count goes to zero, and release */
  575. if (decodes_unlocked) {
  576. if (decodes_unlocked & VGA_RSRC_LEGACY_IO)
  577. vgadev->io_lock_cnt = 0;
  578. if (decodes_unlocked & VGA_RSRC_LEGACY_MEM)
  579. vgadev->mem_lock_cnt = 0;
  580. __vga_put(vgadev, decodes_unlocked);
  581. }
  582. /* change decodes counter */
  583. if (old_decodes & VGA_RSRC_LEGACY_MASK &&
  584. !(new_decodes & VGA_RSRC_LEGACY_MASK))
  585. vga_decode_count--;
  586. if (!(old_decodes & VGA_RSRC_LEGACY_MASK) &&
  587. new_decodes & VGA_RSRC_LEGACY_MASK)
  588. vga_decode_count++;
  589. pr_debug("decoding count now is: %d\n", vga_decode_count);
  590. }
  591. static void __vga_set_legacy_decoding(struct pci_dev *pdev,
  592. unsigned int decodes,
  593. bool userspace)
  594. {
  595. struct vga_device *vgadev;
  596. unsigned long flags;
  597. decodes &= VGA_RSRC_LEGACY_MASK;
  598. spin_lock_irqsave(&vga_lock, flags);
  599. vgadev = vgadev_find(pdev);
  600. if (vgadev == NULL)
  601. goto bail;
  602. /* don't let userspace futz with kernel driver decodes */
  603. if (userspace && vgadev->set_vga_decode)
  604. goto bail;
  605. /* update the device decodes + counter */
  606. vga_update_device_decodes(vgadev, decodes);
  607. /* XXX if somebody is going from "doesn't decode" to "decodes" state
  608. * here, additional care must be taken as we may have pending owner
  609. * ship of non-legacy region ...
  610. */
  611. bail:
  612. spin_unlock_irqrestore(&vga_lock, flags);
  613. }
  614. void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
  615. {
  616. __vga_set_legacy_decoding(pdev, decodes, false);
  617. }
  618. EXPORT_SYMBOL(vga_set_legacy_decoding);
  619. /* call with NULL to unregister */
  620. int vga_client_register(struct pci_dev *pdev, void *cookie,
  621. void (*irq_set_state)(void *cookie, bool state),
  622. unsigned int (*set_vga_decode)(void *cookie,
  623. bool decode))
  624. {
  625. int ret = -ENODEV;
  626. struct vga_device *vgadev;
  627. unsigned long flags;
  628. spin_lock_irqsave(&vga_lock, flags);
  629. vgadev = vgadev_find(pdev);
  630. if (!vgadev)
  631. goto bail;
  632. vgadev->irq_set_state = irq_set_state;
  633. vgadev->set_vga_decode = set_vga_decode;
  634. vgadev->cookie = cookie;
  635. ret = 0;
  636. bail:
  637. spin_unlock_irqrestore(&vga_lock, flags);
  638. return ret;
  639. }
  640. EXPORT_SYMBOL(vga_client_register);
  641. /*
  642. * Char driver implementation
  643. *
  644. * Semantics is:
  645. *
  646. * open : open user instance of the arbitrer. by default, it's
  647. * attached to the default VGA device of the system.
  648. *
  649. * close : close user instance, release locks
  650. *
  651. * read : return a string indicating the status of the target.
  652. * an IO state string is of the form {io,mem,io+mem,none},
  653. * mc and ic are respectively mem and io lock counts (for
  654. * debugging/diagnostic only). "decodes" indicate what the
  655. * card currently decodes, "owns" indicates what is currently
  656. * enabled on it, and "locks" indicates what is locked by this
  657. * card. If the card is unplugged, we get "invalid" then for
  658. * card_ID and an -ENODEV error is returned for any command
  659. * until a new card is targeted
  660. *
  661. * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
  662. *
  663. * write : write a command to the arbiter. List of commands is:
  664. *
  665. * target <card_ID> : switch target to card <card_ID> (see below)
  666. * lock <io_state> : acquires locks on target ("none" is invalid io_state)
  667. * trylock <io_state> : non-blocking acquire locks on target
  668. * unlock <io_state> : release locks on target
  669. * unlock all : release all locks on target held by this user
  670. * decodes <io_state> : set the legacy decoding attributes for the card
  671. *
  672. * poll : event if something change on any card (not just the target)
  673. *
  674. * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
  675. * to go back to the system default card (TODO: not implemented yet).
  676. * Currently, only PCI is supported as a prefix, but the userland API may
  677. * support other bus types in the future, even if the current kernel
  678. * implementation doesn't.
  679. *
  680. * Note about locks:
  681. *
  682. * The driver keeps track of which user has what locks on which card. It
  683. * supports stacking, like the kernel one. This complexifies the implementation
  684. * a bit, but makes the arbiter more tolerant to userspace problems and able
  685. * to properly cleanup in all cases when a process dies.
  686. * Currently, a max of 16 cards simultaneously can have locks issued from
  687. * userspace for a given user (file descriptor instance) of the arbiter.
  688. *
  689. * If the device is hot-unplugged, there is a hook inside the module to notify
  690. * they being added/removed in the system and automatically added/removed in
  691. * the arbiter.
  692. */
  693. #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
  694. #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
  695. /*
  696. * Each user has an array of these, tracking which cards have locks
  697. */
  698. struct vga_arb_user_card {
  699. struct pci_dev *pdev;
  700. unsigned int mem_cnt;
  701. unsigned int io_cnt;
  702. };
  703. struct vga_arb_private {
  704. struct list_head list;
  705. struct pci_dev *target;
  706. struct vga_arb_user_card cards[MAX_USER_CARDS];
  707. spinlock_t lock;
  708. };
  709. static LIST_HEAD(vga_user_list);
  710. static DEFINE_SPINLOCK(vga_user_lock);
  711. /*
  712. * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
  713. * returns the respective values. If the string is not in this format,
  714. * it returns 0.
  715. */
  716. static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
  717. unsigned int *bus, unsigned int *devfn)
  718. {
  719. int n;
  720. unsigned int slot, func;
  721. n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
  722. if (n != 4)
  723. return 0;
  724. *devfn = PCI_DEVFN(slot, func);
  725. return 1;
  726. }
  727. static ssize_t vga_arb_read(struct file *file, char __user *buf,
  728. size_t count, loff_t *ppos)
  729. {
  730. struct vga_arb_private *priv = file->private_data;
  731. struct vga_device *vgadev;
  732. struct pci_dev *pdev;
  733. unsigned long flags;
  734. size_t len;
  735. int rc;
  736. char *lbuf;
  737. lbuf = kmalloc(1024, GFP_KERNEL);
  738. if (lbuf == NULL)
  739. return -ENOMEM;
  740. /* Shields against vga_arb_device_card_gone (pci_dev going
  741. * away), and allows access to vga list
  742. */
  743. spin_lock_irqsave(&vga_lock, flags);
  744. /* If we are targeting the default, use it */
  745. pdev = priv->target;
  746. if (pdev == NULL || pdev == PCI_INVALID_CARD) {
  747. spin_unlock_irqrestore(&vga_lock, flags);
  748. len = sprintf(lbuf, "invalid");
  749. goto done;
  750. }
  751. /* Find card vgadev structure */
  752. vgadev = vgadev_find(pdev);
  753. if (vgadev == NULL) {
  754. /* Wow, it's not in the list, that shouldn't happen,
  755. * let's fix us up and return invalid card
  756. */
  757. if (pdev == priv->target)
  758. vga_arb_device_card_gone(pdev);
  759. spin_unlock_irqrestore(&vga_lock, flags);
  760. len = sprintf(lbuf, "invalid");
  761. goto done;
  762. }
  763. /* Fill the buffer with infos */
  764. len = snprintf(lbuf, 1024,
  765. "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
  766. vga_decode_count, pci_name(pdev),
  767. vga_iostate_to_str(vgadev->decodes),
  768. vga_iostate_to_str(vgadev->owns),
  769. vga_iostate_to_str(vgadev->locks),
  770. vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
  771. spin_unlock_irqrestore(&vga_lock, flags);
  772. done:
  773. /* Copy that to user */
  774. if (len > count)
  775. len = count;
  776. rc = copy_to_user(buf, lbuf, len);
  777. kfree(lbuf);
  778. if (rc)
  779. return -EFAULT;
  780. return len;
  781. }
  782. /*
  783. * TODO: To avoid parsing inside kernel and to improve the speed we may
  784. * consider use ioctl here
  785. */
  786. static ssize_t vga_arb_write(struct file *file, const char __user *buf,
  787. size_t count, loff_t *ppos)
  788. {
  789. struct vga_arb_private *priv = file->private_data;
  790. struct vga_arb_user_card *uc = NULL;
  791. struct pci_dev *pdev;
  792. unsigned int io_state;
  793. char *kbuf, *curr_pos;
  794. size_t remaining = count;
  795. int ret_val;
  796. int i;
  797. kbuf = kmalloc(count + 1, GFP_KERNEL);
  798. if (!kbuf)
  799. return -ENOMEM;
  800. if (copy_from_user(kbuf, buf, count)) {
  801. kfree(kbuf);
  802. return -EFAULT;
  803. }
  804. curr_pos = kbuf;
  805. kbuf[count] = '\0'; /* Just to make sure... */
  806. if (strncmp(curr_pos, "lock ", 5) == 0) {
  807. curr_pos += 5;
  808. remaining -= 5;
  809. pr_debug("client 0x%p called 'lock'\n", priv);
  810. if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
  811. ret_val = -EPROTO;
  812. goto done;
  813. }
  814. if (io_state == VGA_RSRC_NONE) {
  815. ret_val = -EPROTO;
  816. goto done;
  817. }
  818. pdev = priv->target;
  819. if (priv->target == NULL) {
  820. ret_val = -ENODEV;
  821. goto done;
  822. }
  823. vga_get_uninterruptible(pdev, io_state);
  824. /* Update the client's locks lists... */
  825. for (i = 0; i < MAX_USER_CARDS; i++) {
  826. if (priv->cards[i].pdev == pdev) {
  827. if (io_state & VGA_RSRC_LEGACY_IO)
  828. priv->cards[i].io_cnt++;
  829. if (io_state & VGA_RSRC_LEGACY_MEM)
  830. priv->cards[i].mem_cnt++;
  831. break;
  832. }
  833. }
  834. ret_val = count;
  835. goto done;
  836. } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
  837. curr_pos += 7;
  838. remaining -= 7;
  839. pr_debug("client 0x%p called 'unlock'\n", priv);
  840. if (strncmp(curr_pos, "all", 3) == 0)
  841. io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  842. else {
  843. if (!vga_str_to_iostate
  844. (curr_pos, remaining, &io_state)) {
  845. ret_val = -EPROTO;
  846. goto done;
  847. }
  848. /* TODO: Add this?
  849. if (io_state == VGA_RSRC_NONE) {
  850. ret_val = -EPROTO;
  851. goto done;
  852. }
  853. */
  854. }
  855. pdev = priv->target;
  856. if (priv->target == NULL) {
  857. ret_val = -ENODEV;
  858. goto done;
  859. }
  860. for (i = 0; i < MAX_USER_CARDS; i++) {
  861. if (priv->cards[i].pdev == pdev)
  862. uc = &priv->cards[i];
  863. }
  864. if (!uc) {
  865. ret_val = -EINVAL;
  866. goto done;
  867. }
  868. if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
  869. ret_val = -EINVAL;
  870. goto done;
  871. }
  872. if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
  873. ret_val = -EINVAL;
  874. goto done;
  875. }
  876. vga_put(pdev, io_state);
  877. if (io_state & VGA_RSRC_LEGACY_IO)
  878. uc->io_cnt--;
  879. if (io_state & VGA_RSRC_LEGACY_MEM)
  880. uc->mem_cnt--;
  881. ret_val = count;
  882. goto done;
  883. } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
  884. curr_pos += 8;
  885. remaining -= 8;
  886. pr_debug("client 0x%p called 'trylock'\n", priv);
  887. if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
  888. ret_val = -EPROTO;
  889. goto done;
  890. }
  891. /* TODO: Add this?
  892. if (io_state == VGA_RSRC_NONE) {
  893. ret_val = -EPROTO;
  894. goto done;
  895. }
  896. */
  897. pdev = priv->target;
  898. if (priv->target == NULL) {
  899. ret_val = -ENODEV;
  900. goto done;
  901. }
  902. if (vga_tryget(pdev, io_state)) {
  903. /* Update the client's locks lists... */
  904. for (i = 0; i < MAX_USER_CARDS; i++) {
  905. if (priv->cards[i].pdev == pdev) {
  906. if (io_state & VGA_RSRC_LEGACY_IO)
  907. priv->cards[i].io_cnt++;
  908. if (io_state & VGA_RSRC_LEGACY_MEM)
  909. priv->cards[i].mem_cnt++;
  910. break;
  911. }
  912. }
  913. ret_val = count;
  914. goto done;
  915. } else {
  916. ret_val = -EBUSY;
  917. goto done;
  918. }
  919. } else if (strncmp(curr_pos, "target ", 7) == 0) {
  920. unsigned int domain, bus, devfn;
  921. struct vga_device *vgadev;
  922. curr_pos += 7;
  923. remaining -= 7;
  924. pr_debug("client 0x%p called 'target'\n", priv);
  925. /* if target is default */
  926. if (!strncmp(curr_pos, "default", 7))
  927. pdev = pci_dev_get(vga_default_device());
  928. else {
  929. if (!vga_pci_str_to_vars(curr_pos, remaining,
  930. &domain, &bus, &devfn)) {
  931. ret_val = -EPROTO;
  932. goto done;
  933. }
  934. pr_debug("%s ==> %x:%x:%x.%x\n", curr_pos,
  935. domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
  936. pdev = pci_get_domain_bus_and_slot(domain, bus, devfn);
  937. pr_debug("pdev %p\n", pdev);
  938. if (!pdev) {
  939. pr_err("invalid PCI address %x:%x:%x\n",
  940. domain, bus, devfn);
  941. ret_val = -ENODEV;
  942. goto done;
  943. }
  944. }
  945. vgadev = vgadev_find(pdev);
  946. pr_debug("vgadev %p\n", vgadev);
  947. if (vgadev == NULL) {
  948. if (pdev) {
  949. pr_err("this pci device is not a vga device\n");
  950. pci_dev_put(pdev);
  951. }
  952. ret_val = -ENODEV;
  953. goto done;
  954. }
  955. priv->target = pdev;
  956. for (i = 0; i < MAX_USER_CARDS; i++) {
  957. if (priv->cards[i].pdev == pdev)
  958. break;
  959. if (priv->cards[i].pdev == NULL) {
  960. priv->cards[i].pdev = pdev;
  961. priv->cards[i].io_cnt = 0;
  962. priv->cards[i].mem_cnt = 0;
  963. break;
  964. }
  965. }
  966. if (i == MAX_USER_CARDS) {
  967. pr_err("maximum user cards (%d) number reached!\n",
  968. MAX_USER_CARDS);
  969. pci_dev_put(pdev);
  970. /* XXX: which value to return? */
  971. ret_val = -ENOMEM;
  972. goto done;
  973. }
  974. ret_val = count;
  975. pci_dev_put(pdev);
  976. goto done;
  977. } else if (strncmp(curr_pos, "decodes ", 8) == 0) {
  978. curr_pos += 8;
  979. remaining -= 8;
  980. pr_debug("client 0x%p called 'decodes'\n", priv);
  981. if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
  982. ret_val = -EPROTO;
  983. goto done;
  984. }
  985. pdev = priv->target;
  986. if (priv->target == NULL) {
  987. ret_val = -ENODEV;
  988. goto done;
  989. }
  990. __vga_set_legacy_decoding(pdev, io_state, true);
  991. ret_val = count;
  992. goto done;
  993. }
  994. /* If we got here, the message written is not part of the protocol! */
  995. kfree(kbuf);
  996. return -EPROTO;
  997. done:
  998. kfree(kbuf);
  999. return ret_val;
  1000. }
  1001. static unsigned int vga_arb_fpoll(struct file *file, poll_table *wait)
  1002. {
  1003. pr_debug("%s\n", __func__);
  1004. poll_wait(file, &vga_wait_queue, wait);
  1005. return POLLIN;
  1006. }
  1007. static int vga_arb_open(struct inode *inode, struct file *file)
  1008. {
  1009. struct vga_arb_private *priv;
  1010. unsigned long flags;
  1011. pr_debug("%s\n", __func__);
  1012. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1013. if (priv == NULL)
  1014. return -ENOMEM;
  1015. spin_lock_init(&priv->lock);
  1016. file->private_data = priv;
  1017. spin_lock_irqsave(&vga_user_lock, flags);
  1018. list_add(&priv->list, &vga_user_list);
  1019. spin_unlock_irqrestore(&vga_user_lock, flags);
  1020. /* Set the client' lists of locks */
  1021. priv->target = vga_default_device(); /* Maybe this is still null! */
  1022. priv->cards[0].pdev = priv->target;
  1023. priv->cards[0].io_cnt = 0;
  1024. priv->cards[0].mem_cnt = 0;
  1025. return 0;
  1026. }
  1027. static int vga_arb_release(struct inode *inode, struct file *file)
  1028. {
  1029. struct vga_arb_private *priv = file->private_data;
  1030. struct vga_arb_user_card *uc;
  1031. unsigned long flags;
  1032. int i;
  1033. pr_debug("%s\n", __func__);
  1034. spin_lock_irqsave(&vga_user_lock, flags);
  1035. list_del(&priv->list);
  1036. for (i = 0; i < MAX_USER_CARDS; i++) {
  1037. uc = &priv->cards[i];
  1038. if (uc->pdev == NULL)
  1039. continue;
  1040. pr_debug("uc->io_cnt == %d, uc->mem_cnt == %d\n",
  1041. uc->io_cnt, uc->mem_cnt);
  1042. while (uc->io_cnt--)
  1043. vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
  1044. while (uc->mem_cnt--)
  1045. vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
  1046. }
  1047. spin_unlock_irqrestore(&vga_user_lock, flags);
  1048. kfree(priv);
  1049. return 0;
  1050. }
  1051. static void vga_arb_device_card_gone(struct pci_dev *pdev)
  1052. {
  1053. }
  1054. /*
  1055. * callback any registered clients to let them know we have a
  1056. * change in VGA cards
  1057. */
  1058. static void vga_arbiter_notify_clients(void)
  1059. {
  1060. struct vga_device *vgadev;
  1061. unsigned long flags;
  1062. uint32_t new_decodes;
  1063. bool new_state;
  1064. if (!vga_arbiter_used)
  1065. return;
  1066. spin_lock_irqsave(&vga_lock, flags);
  1067. list_for_each_entry(vgadev, &vga_list, list) {
  1068. if (vga_count > 1)
  1069. new_state = false;
  1070. else
  1071. new_state = true;
  1072. if (vgadev->set_vga_decode) {
  1073. new_decodes = vgadev->set_vga_decode(vgadev->cookie,
  1074. new_state);
  1075. vga_update_device_decodes(vgadev, new_decodes);
  1076. }
  1077. }
  1078. spin_unlock_irqrestore(&vga_lock, flags);
  1079. }
  1080. static int pci_notify(struct notifier_block *nb, unsigned long action,
  1081. void *data)
  1082. {
  1083. struct device *dev = data;
  1084. struct pci_dev *pdev = to_pci_dev(dev);
  1085. bool notify = false;
  1086. pr_debug("%s\n", __func__);
  1087. /* For now we're only intereted in devices added and removed. I didn't
  1088. * test this thing here, so someone needs to double check for the
  1089. * cases of hotplugable vga cards. */
  1090. if (action == BUS_NOTIFY_ADD_DEVICE)
  1091. notify = vga_arbiter_add_pci_device(pdev);
  1092. else if (action == BUS_NOTIFY_DEL_DEVICE)
  1093. notify = vga_arbiter_del_pci_device(pdev);
  1094. if (notify)
  1095. vga_arbiter_notify_clients();
  1096. return 0;
  1097. }
  1098. static struct notifier_block pci_notifier = {
  1099. .notifier_call = pci_notify,
  1100. };
  1101. static const struct file_operations vga_arb_device_fops = {
  1102. .read = vga_arb_read,
  1103. .write = vga_arb_write,
  1104. .poll = vga_arb_fpoll,
  1105. .open = vga_arb_open,
  1106. .release = vga_arb_release,
  1107. .llseek = noop_llseek,
  1108. };
  1109. static struct miscdevice vga_arb_device = {
  1110. MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
  1111. };
  1112. static int __init vga_arb_device_init(void)
  1113. {
  1114. int rc;
  1115. struct pci_dev *pdev;
  1116. struct vga_device *vgadev;
  1117. rc = misc_register(&vga_arb_device);
  1118. if (rc < 0)
  1119. pr_err("error %d registering device\n", rc);
  1120. bus_register_notifier(&pci_bus_type, &pci_notifier);
  1121. /* We add all pci devices satisfying vga class in the arbiter by
  1122. * default */
  1123. pdev = NULL;
  1124. while ((pdev =
  1125. pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  1126. PCI_ANY_ID, pdev)) != NULL)
  1127. vga_arbiter_add_pci_device(pdev);
  1128. pr_info("loaded\n");
  1129. list_for_each_entry(vgadev, &vga_list, list) {
  1130. #if defined(CONFIG_X86) || defined(CONFIG_IA64)
  1131. /*
  1132. * Override vga_arbiter_add_pci_device()'s I/O based detection
  1133. * as it may take the wrong device (e.g. on Apple system under
  1134. * EFI).
  1135. *
  1136. * Select the device owning the boot framebuffer if there is
  1137. * one.
  1138. */
  1139. resource_size_t start, end, limit;
  1140. unsigned long flags;
  1141. int i;
  1142. limit = screen_info.lfb_base + screen_info.lfb_size;
  1143. /* Does firmware framebuffer belong to us? */
  1144. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1145. flags = pci_resource_flags(vgadev->pdev, i);
  1146. if ((flags & IORESOURCE_MEM) == 0)
  1147. continue;
  1148. start = pci_resource_start(vgadev->pdev, i);
  1149. end = pci_resource_end(vgadev->pdev, i);
  1150. if (!start || !end)
  1151. continue;
  1152. if (screen_info.lfb_base < start || limit >= end)
  1153. continue;
  1154. if (!vga_default_device())
  1155. pr_info("setting as boot device: PCI:%s\n",
  1156. pci_name(vgadev->pdev));
  1157. else if (vgadev->pdev != vga_default_device())
  1158. pr_info("overriding boot device: PCI:%s\n",
  1159. pci_name(vgadev->pdev));
  1160. vga_set_default_device(vgadev->pdev);
  1161. }
  1162. #endif
  1163. if (vgadev->bridge_has_one_vga)
  1164. pr_info("bridge control possible %s\n",
  1165. pci_name(vgadev->pdev));
  1166. else
  1167. pr_info("no bridge control possible %s\n",
  1168. pci_name(vgadev->pdev));
  1169. }
  1170. return rc;
  1171. }
  1172. subsys_initcall(vga_arb_device_init);