vgaarb.c 41 KB

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