vgaarb.c 42 KB

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