vgaarb.c 40 KB

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