vgaarb.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /*
  2. * vgaarb.c
  3. *
  4. * (C) Copyright 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  5. * (C) Copyright 2007 Paulo R. Zanoni <przanoni@gmail.com>
  6. * (C) Copyright 2007, 2009 Tiago Vignatti <vignatti@freedesktop.org>
  7. *
  8. * Implements the VGA arbitration. For details refer to
  9. * Documentation/vgaarbiter.txt
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/pci.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/list.h>
  17. #include <linux/sched.h>
  18. #include <linux/wait.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/poll.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/slab.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/vgaarb.h>
  25. static void vga_arbiter_notify_clients(void);
  26. /*
  27. * We keep a list of all vga devices in the system to speed
  28. * up the various operations of the arbiter
  29. */
  30. struct vga_device {
  31. struct list_head list;
  32. struct pci_dev *pdev;
  33. unsigned int decodes; /* what does it decodes */
  34. unsigned int owns; /* what does it owns */
  35. unsigned int locks; /* what does it locks */
  36. unsigned int io_lock_cnt; /* legacy IO lock count */
  37. unsigned int mem_lock_cnt; /* legacy MEM lock count */
  38. unsigned int io_norm_cnt; /* normal IO count */
  39. unsigned int mem_norm_cnt; /* normal MEM count */
  40. /* allow IRQ enable/disable hook */
  41. void *cookie;
  42. void (*irq_set_state)(void *cookie, bool enable);
  43. unsigned int (*set_vga_decode)(void *cookie, bool decode);
  44. };
  45. static LIST_HEAD(vga_list);
  46. static int vga_count, vga_decode_count;
  47. static bool vga_arbiter_used;
  48. static DEFINE_SPINLOCK(vga_lock);
  49. static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
  50. static const char *vga_iostate_to_str(unsigned int iostate)
  51. {
  52. /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
  53. iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  54. switch (iostate) {
  55. case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
  56. return "io+mem";
  57. case VGA_RSRC_LEGACY_IO:
  58. return "io";
  59. case VGA_RSRC_LEGACY_MEM:
  60. return "mem";
  61. }
  62. return "none";
  63. }
  64. static int vga_str_to_iostate(char *buf, int str_size, int *io_state)
  65. {
  66. /* we could in theory hand out locks on IO and mem
  67. * separately to userspace but it can cause deadlocks */
  68. if (strncmp(buf, "none", 4) == 0) {
  69. *io_state = VGA_RSRC_NONE;
  70. return 1;
  71. }
  72. /* XXX We're not chekcing the str_size! */
  73. if (strncmp(buf, "io+mem", 6) == 0)
  74. goto both;
  75. else if (strncmp(buf, "io", 2) == 0)
  76. goto both;
  77. else if (strncmp(buf, "mem", 3) == 0)
  78. goto both;
  79. return 0;
  80. both:
  81. *io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  82. return 1;
  83. }
  84. #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
  85. /* this is only used a cookie - it should not be dereferenced */
  86. static struct pci_dev *vga_default;
  87. #endif
  88. static void vga_arb_device_card_gone(struct pci_dev *pdev);
  89. /* Find somebody in our list */
  90. static struct vga_device *vgadev_find(struct pci_dev *pdev)
  91. {
  92. struct vga_device *vgadev;
  93. list_for_each_entry(vgadev, &vga_list, list)
  94. if (pdev == vgadev->pdev)
  95. return vgadev;
  96. return NULL;
  97. }
  98. /* Returns the default VGA device (vgacon's babe) */
  99. #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
  100. struct pci_dev *vga_default_device(void)
  101. {
  102. return vga_default;
  103. }
  104. #endif
  105. static inline void vga_irq_set_state(struct vga_device *vgadev, bool state)
  106. {
  107. if (vgadev->irq_set_state)
  108. vgadev->irq_set_state(vgadev->cookie, state);
  109. }
  110. /* If we don't ever use VGA arb we should avoid
  111. turning off anything anywhere due to old X servers getting
  112. confused about the boot device not being VGA */
  113. static void vga_check_first_use(void)
  114. {
  115. /* we should inform all GPUs in the system that
  116. * VGA arb has occured and to try and disable resources
  117. * if they can */
  118. if (!vga_arbiter_used) {
  119. vga_arbiter_used = true;
  120. vga_arbiter_notify_clients();
  121. }
  122. }
  123. static struct vga_device *__vga_tryget(struct vga_device *vgadev,
  124. unsigned int rsrc)
  125. {
  126. unsigned int wants, legacy_wants, match;
  127. struct vga_device *conflict;
  128. unsigned int pci_bits;
  129. /* Account for "normal" resources to lock. If we decode the legacy,
  130. * counterpart, we need to request it as well
  131. */
  132. if ((rsrc & VGA_RSRC_NORMAL_IO) &&
  133. (vgadev->decodes & VGA_RSRC_LEGACY_IO))
  134. rsrc |= VGA_RSRC_LEGACY_IO;
  135. if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
  136. (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
  137. rsrc |= VGA_RSRC_LEGACY_MEM;
  138. pr_devel("%s: %d\n", __func__, rsrc);
  139. pr_devel("%s: owns: %d\n", __func__, vgadev->owns);
  140. /* Check what resources we need to acquire */
  141. wants = rsrc & ~vgadev->owns;
  142. /* We already own everything, just mark locked & bye bye */
  143. if (wants == 0)
  144. goto lock_them;
  145. /* We don't need to request a legacy resource, we just enable
  146. * appropriate decoding and go
  147. */
  148. legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
  149. if (legacy_wants == 0)
  150. goto enable_them;
  151. /* Ok, we don't, let's find out how we need to kick off */
  152. list_for_each_entry(conflict, &vga_list, list) {
  153. unsigned int lwants = legacy_wants;
  154. unsigned int change_bridge = 0;
  155. /* Don't conflict with myself */
  156. if (vgadev == conflict)
  157. continue;
  158. /* Check if the architecture allows a conflict between those
  159. * 2 devices or if they are on separate domains
  160. */
  161. if (!vga_conflicts(vgadev->pdev, conflict->pdev))
  162. continue;
  163. /* We have a possible conflict. before we go further, we must
  164. * check if we sit on the same bus as the conflicting device.
  165. * if we don't, then we must tie both IO and MEM resources
  166. * together since there is only a single bit controlling
  167. * VGA forwarding on P2P bridges
  168. */
  169. if (vgadev->pdev->bus != conflict->pdev->bus) {
  170. change_bridge = 1;
  171. lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  172. }
  173. /* Check if the guy has a lock on the resource. If he does,
  174. * return the conflicting entry
  175. */
  176. if (conflict->locks & lwants)
  177. return conflict;
  178. /* Ok, now check if he owns the resource we want. We don't need
  179. * to check "decodes" since it should be impossible to own
  180. * own legacy resources you don't decode unless I have a bug
  181. * in this code...
  182. */
  183. WARN_ON(conflict->owns & ~conflict->decodes);
  184. match = lwants & conflict->owns;
  185. if (!match)
  186. continue;
  187. /* looks like he doesn't have a lock, we can steal
  188. * them from him
  189. */
  190. vga_irq_set_state(conflict, false);
  191. pci_bits = 0;
  192. if (lwants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
  193. pci_bits |= PCI_COMMAND_MEMORY;
  194. if (lwants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
  195. pci_bits |= PCI_COMMAND_IO;
  196. pci_set_vga_state(conflict->pdev, false, pci_bits,
  197. change_bridge);
  198. conflict->owns &= ~lwants;
  199. /* If he also owned non-legacy, that is no longer the case */
  200. if (lwants & VGA_RSRC_LEGACY_MEM)
  201. conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
  202. if (lwants & VGA_RSRC_LEGACY_IO)
  203. conflict->owns &= ~VGA_RSRC_NORMAL_IO;
  204. }
  205. enable_them:
  206. /* ok dude, we got it, everybody conflicting has been disabled, let's
  207. * enable us. Make sure we don't mark a bit in "owns" that we don't
  208. * also have in "decodes". We can lock resources we don't decode but
  209. * not own them.
  210. */
  211. pci_bits = 0;
  212. if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
  213. pci_bits |= PCI_COMMAND_MEMORY;
  214. if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
  215. pci_bits |= PCI_COMMAND_IO;
  216. pci_set_vga_state(vgadev->pdev, true, pci_bits, !!(wants & VGA_RSRC_LEGACY_MASK));
  217. vga_irq_set_state(vgadev, true);
  218. vgadev->owns |= (wants & vgadev->decodes);
  219. lock_them:
  220. vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
  221. if (rsrc & VGA_RSRC_LEGACY_IO)
  222. vgadev->io_lock_cnt++;
  223. if (rsrc & VGA_RSRC_LEGACY_MEM)
  224. vgadev->mem_lock_cnt++;
  225. if (rsrc & VGA_RSRC_NORMAL_IO)
  226. vgadev->io_norm_cnt++;
  227. if (rsrc & VGA_RSRC_NORMAL_MEM)
  228. vgadev->mem_norm_cnt++;
  229. return NULL;
  230. }
  231. static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
  232. {
  233. unsigned int old_locks = vgadev->locks;
  234. pr_devel("%s\n", __func__);
  235. /* Update our counters, and account for equivalent legacy resources
  236. * if we decode them
  237. */
  238. if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
  239. vgadev->io_norm_cnt--;
  240. if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
  241. rsrc |= VGA_RSRC_LEGACY_IO;
  242. }
  243. if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
  244. vgadev->mem_norm_cnt--;
  245. if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
  246. rsrc |= VGA_RSRC_LEGACY_MEM;
  247. }
  248. if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
  249. vgadev->io_lock_cnt--;
  250. if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
  251. vgadev->mem_lock_cnt--;
  252. /* Just clear lock bits, we do lazy operations so we don't really
  253. * have to bother about anything else at this point
  254. */
  255. if (vgadev->io_lock_cnt == 0)
  256. vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
  257. if (vgadev->mem_lock_cnt == 0)
  258. vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
  259. /* Kick the wait queue in case somebody was waiting if we actually
  260. * released something
  261. */
  262. if (old_locks != vgadev->locks)
  263. wake_up_all(&vga_wait_queue);
  264. }
  265. int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
  266. {
  267. struct vga_device *vgadev, *conflict;
  268. unsigned long flags;
  269. wait_queue_t wait;
  270. int rc = 0;
  271. vga_check_first_use();
  272. /* The one who calls us should check for this, but lets be sure... */
  273. if (pdev == NULL)
  274. pdev = vga_default_device();
  275. if (pdev == NULL)
  276. return 0;
  277. for (;;) {
  278. spin_lock_irqsave(&vga_lock, flags);
  279. vgadev = vgadev_find(pdev);
  280. if (vgadev == NULL) {
  281. spin_unlock_irqrestore(&vga_lock, flags);
  282. rc = -ENODEV;
  283. break;
  284. }
  285. conflict = __vga_tryget(vgadev, rsrc);
  286. spin_unlock_irqrestore(&vga_lock, flags);
  287. if (conflict == NULL)
  288. break;
  289. /* We have a conflict, we wait until somebody kicks the
  290. * work queue. Currently we have one work queue that we
  291. * kick each time some resources are released, but it would
  292. * be fairly easy to have a per device one so that we only
  293. * need to attach to the conflicting device
  294. */
  295. init_waitqueue_entry(&wait, current);
  296. add_wait_queue(&vga_wait_queue, &wait);
  297. set_current_state(interruptible ?
  298. TASK_INTERRUPTIBLE :
  299. TASK_UNINTERRUPTIBLE);
  300. if (signal_pending(current)) {
  301. rc = -EINTR;
  302. break;
  303. }
  304. schedule();
  305. remove_wait_queue(&vga_wait_queue, &wait);
  306. set_current_state(TASK_RUNNING);
  307. }
  308. return rc;
  309. }
  310. EXPORT_SYMBOL(vga_get);
  311. int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
  312. {
  313. struct vga_device *vgadev;
  314. unsigned long flags;
  315. int rc = 0;
  316. vga_check_first_use();
  317. /* The one who calls us should check for this, but lets be sure... */
  318. if (pdev == NULL)
  319. pdev = vga_default_device();
  320. if (pdev == NULL)
  321. return 0;
  322. spin_lock_irqsave(&vga_lock, flags);
  323. vgadev = vgadev_find(pdev);
  324. if (vgadev == NULL) {
  325. rc = -ENODEV;
  326. goto bail;
  327. }
  328. if (__vga_tryget(vgadev, rsrc))
  329. rc = -EBUSY;
  330. bail:
  331. spin_unlock_irqrestore(&vga_lock, flags);
  332. return rc;
  333. }
  334. EXPORT_SYMBOL(vga_tryget);
  335. void vga_put(struct pci_dev *pdev, unsigned int rsrc)
  336. {
  337. struct vga_device *vgadev;
  338. unsigned long flags;
  339. /* The one who calls us should check for this, but lets be sure... */
  340. if (pdev == NULL)
  341. pdev = vga_default_device();
  342. if (pdev == NULL)
  343. return;
  344. spin_lock_irqsave(&vga_lock, flags);
  345. vgadev = vgadev_find(pdev);
  346. if (vgadev == NULL)
  347. goto bail;
  348. __vga_put(vgadev, rsrc);
  349. bail:
  350. spin_unlock_irqrestore(&vga_lock, flags);
  351. }
  352. EXPORT_SYMBOL(vga_put);
  353. /*
  354. * Currently, we assume that the "initial" setup of the system is
  355. * not sane, that is we come up with conflicting devices and let
  356. * the arbiter's client decides if devices decodes or not legacy
  357. * things.
  358. */
  359. static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
  360. {
  361. struct vga_device *vgadev;
  362. unsigned long flags;
  363. struct pci_bus *bus;
  364. struct pci_dev *bridge;
  365. u16 cmd;
  366. /* Only deal with VGA class devices */
  367. if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  368. return false;
  369. /* Allocate structure */
  370. vgadev = kmalloc(sizeof(struct vga_device), GFP_KERNEL);
  371. if (vgadev == NULL) {
  372. pr_err("vgaarb: failed to allocate pci device\n");
  373. /* What to do on allocation failure ? For now, let's
  374. * just do nothing, I'm not sure there is anything saner
  375. * to be done
  376. */
  377. return false;
  378. }
  379. memset(vgadev, 0, sizeof(*vgadev));
  380. /* Take lock & check for duplicates */
  381. spin_lock_irqsave(&vga_lock, flags);
  382. if (vgadev_find(pdev) != NULL) {
  383. BUG_ON(1);
  384. goto fail;
  385. }
  386. vgadev->pdev = pdev;
  387. /* By default, assume we decode everything */
  388. vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  389. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  390. /* by default mark it as decoding */
  391. vga_decode_count++;
  392. /* Mark that we "own" resources based on our enables, we will
  393. * clear that below if the bridge isn't forwarding
  394. */
  395. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  396. if (cmd & PCI_COMMAND_IO)
  397. vgadev->owns |= VGA_RSRC_LEGACY_IO;
  398. if (cmd & PCI_COMMAND_MEMORY)
  399. vgadev->owns |= VGA_RSRC_LEGACY_MEM;
  400. /* Check if VGA cycles can get down to us */
  401. bus = pdev->bus;
  402. while (bus) {
  403. bridge = bus->self;
  404. if (bridge) {
  405. u16 l;
  406. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
  407. &l);
  408. if (!(l & PCI_BRIDGE_CTL_VGA)) {
  409. vgadev->owns = 0;
  410. break;
  411. }
  412. }
  413. bus = bus->parent;
  414. }
  415. /* Deal with VGA default device. Use first enabled one
  416. * by default if arch doesn't have it's own hook
  417. */
  418. #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
  419. if (vga_default == NULL &&
  420. ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK))
  421. vga_default = pci_dev_get(pdev);
  422. #endif
  423. /* Add to the list */
  424. list_add(&vgadev->list, &vga_list);
  425. vga_count++;
  426. pr_info("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
  427. pci_name(pdev),
  428. vga_iostate_to_str(vgadev->decodes),
  429. vga_iostate_to_str(vgadev->owns),
  430. vga_iostate_to_str(vgadev->locks));
  431. spin_unlock_irqrestore(&vga_lock, flags);
  432. return true;
  433. fail:
  434. spin_unlock_irqrestore(&vga_lock, flags);
  435. kfree(vgadev);
  436. return false;
  437. }
  438. static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
  439. {
  440. struct vga_device *vgadev;
  441. unsigned long flags;
  442. bool ret = true;
  443. spin_lock_irqsave(&vga_lock, flags);
  444. vgadev = vgadev_find(pdev);
  445. if (vgadev == NULL) {
  446. ret = false;
  447. goto bail;
  448. }
  449. if (vga_default == pdev) {
  450. pci_dev_put(vga_default);
  451. vga_default = NULL;
  452. }
  453. if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
  454. vga_decode_count--;
  455. /* Remove entry from list */
  456. list_del(&vgadev->list);
  457. vga_count--;
  458. /* Notify userland driver that the device is gone so it discards
  459. * it's copies of the pci_dev pointer
  460. */
  461. vga_arb_device_card_gone(pdev);
  462. /* Wake up all possible waiters */
  463. wake_up_all(&vga_wait_queue);
  464. bail:
  465. spin_unlock_irqrestore(&vga_lock, flags);
  466. kfree(vgadev);
  467. return ret;
  468. }
  469. /* this is called with the lock */
  470. static inline void vga_update_device_decodes(struct vga_device *vgadev,
  471. int new_decodes)
  472. {
  473. int old_decodes;
  474. struct vga_device *new_vgadev, *conflict;
  475. old_decodes = vgadev->decodes;
  476. vgadev->decodes = new_decodes;
  477. pr_info("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
  478. pci_name(vgadev->pdev),
  479. vga_iostate_to_str(old_decodes),
  480. vga_iostate_to_str(vgadev->decodes),
  481. vga_iostate_to_str(vgadev->owns));
  482. /* if we own the decodes we should move them along to
  483. another card */
  484. if ((vgadev->owns & old_decodes) && (vga_count > 1)) {
  485. /* set us to own nothing */
  486. vgadev->owns &= ~old_decodes;
  487. list_for_each_entry(new_vgadev, &vga_list, list) {
  488. if ((new_vgadev != vgadev) &&
  489. (new_vgadev->decodes & VGA_RSRC_LEGACY_MASK)) {
  490. pr_info("vgaarb: transferring owner from PCI:%s to PCI:%s\n", pci_name(vgadev->pdev), pci_name(new_vgadev->pdev));
  491. conflict = __vga_tryget(new_vgadev, VGA_RSRC_LEGACY_MASK);
  492. if (!conflict)
  493. __vga_put(new_vgadev, VGA_RSRC_LEGACY_MASK);
  494. break;
  495. }
  496. }
  497. }
  498. /* change decodes counter */
  499. if (old_decodes != new_decodes) {
  500. if (new_decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
  501. vga_decode_count++;
  502. else
  503. vga_decode_count--;
  504. }
  505. }
  506. void __vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes, bool userspace)
  507. {
  508. struct vga_device *vgadev;
  509. unsigned long flags;
  510. decodes &= VGA_RSRC_LEGACY_MASK;
  511. spin_lock_irqsave(&vga_lock, flags);
  512. vgadev = vgadev_find(pdev);
  513. if (vgadev == NULL)
  514. goto bail;
  515. /* don't let userspace futz with kernel driver decodes */
  516. if (userspace && vgadev->set_vga_decode)
  517. goto bail;
  518. /* update the device decodes + counter */
  519. vga_update_device_decodes(vgadev, decodes);
  520. /* XXX if somebody is going from "doesn't decode" to "decodes" state
  521. * here, additional care must be taken as we may have pending owner
  522. * ship of non-legacy region ...
  523. */
  524. bail:
  525. spin_unlock_irqrestore(&vga_lock, flags);
  526. }
  527. void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
  528. {
  529. __vga_set_legacy_decoding(pdev, decodes, false);
  530. }
  531. EXPORT_SYMBOL(vga_set_legacy_decoding);
  532. /* call with NULL to unregister */
  533. int vga_client_register(struct pci_dev *pdev, void *cookie,
  534. void (*irq_set_state)(void *cookie, bool state),
  535. unsigned int (*set_vga_decode)(void *cookie, bool decode))
  536. {
  537. int ret = -1;
  538. struct vga_device *vgadev;
  539. unsigned long flags;
  540. spin_lock_irqsave(&vga_lock, flags);
  541. vgadev = vgadev_find(pdev);
  542. if (!vgadev)
  543. goto bail;
  544. vgadev->irq_set_state = irq_set_state;
  545. vgadev->set_vga_decode = set_vga_decode;
  546. vgadev->cookie = cookie;
  547. ret = 0;
  548. bail:
  549. spin_unlock_irqrestore(&vga_lock, flags);
  550. return ret;
  551. }
  552. EXPORT_SYMBOL(vga_client_register);
  553. /*
  554. * Char driver implementation
  555. *
  556. * Semantics is:
  557. *
  558. * open : open user instance of the arbitrer. by default, it's
  559. * attached to the default VGA device of the system.
  560. *
  561. * close : close user instance, release locks
  562. *
  563. * read : return a string indicating the status of the target.
  564. * an IO state string is of the form {io,mem,io+mem,none},
  565. * mc and ic are respectively mem and io lock counts (for
  566. * debugging/diagnostic only). "decodes" indicate what the
  567. * card currently decodes, "owns" indicates what is currently
  568. * enabled on it, and "locks" indicates what is locked by this
  569. * card. If the card is unplugged, we get "invalid" then for
  570. * card_ID and an -ENODEV error is returned for any command
  571. * until a new card is targeted
  572. *
  573. * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
  574. *
  575. * write : write a command to the arbiter. List of commands is:
  576. *
  577. * target <card_ID> : switch target to card <card_ID> (see below)
  578. * lock <io_state> : acquires locks on target ("none" is invalid io_state)
  579. * trylock <io_state> : non-blocking acquire locks on target
  580. * unlock <io_state> : release locks on target
  581. * unlock all : release all locks on target held by this user
  582. * decodes <io_state> : set the legacy decoding attributes for the card
  583. *
  584. * poll : event if something change on any card (not just the target)
  585. *
  586. * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
  587. * to go back to the system default card (TODO: not implemented yet).
  588. * Currently, only PCI is supported as a prefix, but the userland API may
  589. * support other bus types in the future, even if the current kernel
  590. * implementation doesn't.
  591. *
  592. * Note about locks:
  593. *
  594. * The driver keeps track of which user has what locks on which card. It
  595. * supports stacking, like the kernel one. This complexifies the implementation
  596. * a bit, but makes the arbiter more tolerant to userspace problems and able
  597. * to properly cleanup in all cases when a process dies.
  598. * Currently, a max of 16 cards simultaneously can have locks issued from
  599. * userspace for a given user (file descriptor instance) of the arbiter.
  600. *
  601. * If the device is hot-unplugged, there is a hook inside the module to notify
  602. * they being added/removed in the system and automatically added/removed in
  603. * the arbiter.
  604. */
  605. #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
  606. #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
  607. /*
  608. * Each user has an array of these, tracking which cards have locks
  609. */
  610. struct vga_arb_user_card {
  611. struct pci_dev *pdev;
  612. unsigned int mem_cnt;
  613. unsigned int io_cnt;
  614. };
  615. struct vga_arb_private {
  616. struct list_head list;
  617. struct pci_dev *target;
  618. struct vga_arb_user_card cards[MAX_USER_CARDS];
  619. spinlock_t lock;
  620. };
  621. static LIST_HEAD(vga_user_list);
  622. static DEFINE_SPINLOCK(vga_user_lock);
  623. /*
  624. * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
  625. * returns the respective values. If the string is not in this format,
  626. * it returns 0.
  627. */
  628. static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
  629. unsigned int *bus, unsigned int *devfn)
  630. {
  631. int n;
  632. unsigned int slot, func;
  633. n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
  634. if (n != 4)
  635. return 0;
  636. *devfn = PCI_DEVFN(slot, func);
  637. return 1;
  638. }
  639. static ssize_t vga_arb_read(struct file *file, char __user * buf,
  640. size_t count, loff_t *ppos)
  641. {
  642. struct vga_arb_private *priv = file->private_data;
  643. struct vga_device *vgadev;
  644. struct pci_dev *pdev;
  645. unsigned long flags;
  646. size_t len;
  647. int rc;
  648. char *lbuf;
  649. lbuf = kmalloc(1024, GFP_KERNEL);
  650. if (lbuf == NULL)
  651. return -ENOMEM;
  652. /* Shields against vga_arb_device_card_gone (pci_dev going
  653. * away), and allows access to vga list
  654. */
  655. spin_lock_irqsave(&vga_lock, flags);
  656. /* If we are targetting the default, use it */
  657. pdev = priv->target;
  658. if (pdev == NULL || pdev == PCI_INVALID_CARD) {
  659. spin_unlock_irqrestore(&vga_lock, flags);
  660. len = sprintf(lbuf, "invalid");
  661. goto done;
  662. }
  663. /* Find card vgadev structure */
  664. vgadev = vgadev_find(pdev);
  665. if (vgadev == NULL) {
  666. /* Wow, it's not in the list, that shouldn't happen,
  667. * let's fix us up and return invalid card
  668. */
  669. if (pdev == priv->target)
  670. vga_arb_device_card_gone(pdev);
  671. spin_unlock_irqrestore(&vga_lock, flags);
  672. len = sprintf(lbuf, "invalid");
  673. goto done;
  674. }
  675. /* Fill the buffer with infos */
  676. len = snprintf(lbuf, 1024,
  677. "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
  678. vga_decode_count, pci_name(pdev),
  679. vga_iostate_to_str(vgadev->decodes),
  680. vga_iostate_to_str(vgadev->owns),
  681. vga_iostate_to_str(vgadev->locks),
  682. vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
  683. spin_unlock_irqrestore(&vga_lock, flags);
  684. done:
  685. /* Copy that to user */
  686. if (len > count)
  687. len = count;
  688. rc = copy_to_user(buf, lbuf, len);
  689. kfree(lbuf);
  690. if (rc)
  691. return -EFAULT;
  692. return len;
  693. }
  694. /*
  695. * TODO: To avoid parsing inside kernel and to improve the speed we may
  696. * consider use ioctl here
  697. */
  698. static ssize_t vga_arb_write(struct file *file, const char __user * buf,
  699. size_t count, loff_t *ppos)
  700. {
  701. struct vga_arb_private *priv = file->private_data;
  702. struct vga_arb_user_card *uc = NULL;
  703. struct pci_dev *pdev;
  704. unsigned int io_state;
  705. char *kbuf, *curr_pos;
  706. size_t remaining = count;
  707. int ret_val;
  708. int i;
  709. kbuf = kmalloc(count + 1, GFP_KERNEL);
  710. if (!kbuf)
  711. return -ENOMEM;
  712. if (copy_from_user(kbuf, buf, count)) {
  713. kfree(kbuf);
  714. return -EFAULT;
  715. }
  716. curr_pos = kbuf;
  717. kbuf[count] = '\0'; /* Just to make sure... */
  718. if (strncmp(curr_pos, "lock ", 5) == 0) {
  719. curr_pos += 5;
  720. remaining -= 5;
  721. pr_devel("client 0x%p called 'lock'\n", priv);
  722. if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
  723. ret_val = -EPROTO;
  724. goto done;
  725. }
  726. if (io_state == VGA_RSRC_NONE) {
  727. ret_val = -EPROTO;
  728. goto done;
  729. }
  730. pdev = priv->target;
  731. if (priv->target == NULL) {
  732. ret_val = -ENODEV;
  733. goto done;
  734. }
  735. vga_get_uninterruptible(pdev, io_state);
  736. /* Update the client's locks lists... */
  737. for (i = 0; i < MAX_USER_CARDS; i++) {
  738. if (priv->cards[i].pdev == pdev) {
  739. if (io_state & VGA_RSRC_LEGACY_IO)
  740. priv->cards[i].io_cnt++;
  741. if (io_state & VGA_RSRC_LEGACY_MEM)
  742. priv->cards[i].mem_cnt++;
  743. break;
  744. }
  745. }
  746. ret_val = count;
  747. goto done;
  748. } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
  749. curr_pos += 7;
  750. remaining -= 7;
  751. pr_devel("client 0x%p called 'unlock'\n", priv);
  752. if (strncmp(curr_pos, "all", 3) == 0)
  753. io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  754. else {
  755. if (!vga_str_to_iostate
  756. (curr_pos, remaining, &io_state)) {
  757. ret_val = -EPROTO;
  758. goto done;
  759. }
  760. /* TODO: Add this?
  761. if (io_state == VGA_RSRC_NONE) {
  762. ret_val = -EPROTO;
  763. goto done;
  764. }
  765. */
  766. }
  767. pdev = priv->target;
  768. if (priv->target == NULL) {
  769. ret_val = -ENODEV;
  770. goto done;
  771. }
  772. for (i = 0; i < MAX_USER_CARDS; i++) {
  773. if (priv->cards[i].pdev == pdev)
  774. uc = &priv->cards[i];
  775. }
  776. if (!uc)
  777. return -EINVAL;
  778. if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0)
  779. return -EINVAL;
  780. if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0)
  781. return -EINVAL;
  782. vga_put(pdev, io_state);
  783. if (io_state & VGA_RSRC_LEGACY_IO)
  784. uc->io_cnt--;
  785. if (io_state & VGA_RSRC_LEGACY_MEM)
  786. uc->mem_cnt--;
  787. ret_val = count;
  788. goto done;
  789. } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
  790. curr_pos += 8;
  791. remaining -= 8;
  792. pr_devel("client 0x%p called 'trylock'\n", priv);
  793. if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
  794. ret_val = -EPROTO;
  795. goto done;
  796. }
  797. /* TODO: Add this?
  798. if (io_state == VGA_RSRC_NONE) {
  799. ret_val = -EPROTO;
  800. goto done;
  801. }
  802. */
  803. pdev = priv->target;
  804. if (priv->target == NULL) {
  805. ret_val = -ENODEV;
  806. goto done;
  807. }
  808. if (vga_tryget(pdev, io_state)) {
  809. /* Update the client's locks lists... */
  810. for (i = 0; i < MAX_USER_CARDS; i++) {
  811. if (priv->cards[i].pdev == pdev) {
  812. if (io_state & VGA_RSRC_LEGACY_IO)
  813. priv->cards[i].io_cnt++;
  814. if (io_state & VGA_RSRC_LEGACY_MEM)
  815. priv->cards[i].mem_cnt++;
  816. break;
  817. }
  818. }
  819. ret_val = count;
  820. goto done;
  821. } else {
  822. ret_val = -EBUSY;
  823. goto done;
  824. }
  825. } else if (strncmp(curr_pos, "target ", 7) == 0) {
  826. struct pci_bus *pbus;
  827. unsigned int domain, bus, devfn;
  828. struct vga_device *vgadev;
  829. curr_pos += 7;
  830. remaining -= 7;
  831. pr_devel("client 0x%p called 'target'\n", priv);
  832. /* if target is default */
  833. if (!strncmp(curr_pos, "default", 7))
  834. pdev = pci_dev_get(vga_default_device());
  835. else {
  836. if (!vga_pci_str_to_vars(curr_pos, remaining,
  837. &domain, &bus, &devfn)) {
  838. ret_val = -EPROTO;
  839. goto done;
  840. }
  841. pr_devel("vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos,
  842. domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
  843. pbus = pci_find_bus(domain, bus);
  844. pr_devel("vgaarb: pbus %p\n", pbus);
  845. if (pbus == NULL) {
  846. pr_err("vgaarb: invalid PCI domain and/or bus address %x:%x\n",
  847. domain, bus);
  848. ret_val = -ENODEV;
  849. goto done;
  850. }
  851. pdev = pci_get_slot(pbus, devfn);
  852. pr_devel("vgaarb: pdev %p\n", pdev);
  853. if (!pdev) {
  854. pr_err("vgaarb: invalid PCI address %x:%x\n",
  855. bus, devfn);
  856. ret_val = -ENODEV;
  857. goto done;
  858. }
  859. }
  860. vgadev = vgadev_find(pdev);
  861. pr_devel("vgaarb: vgadev %p\n", vgadev);
  862. if (vgadev == NULL) {
  863. pr_err("vgaarb: this pci device is not a vga device\n");
  864. pci_dev_put(pdev);
  865. ret_val = -ENODEV;
  866. goto done;
  867. }
  868. priv->target = pdev;
  869. for (i = 0; i < MAX_USER_CARDS; i++) {
  870. if (priv->cards[i].pdev == pdev)
  871. break;
  872. if (priv->cards[i].pdev == NULL) {
  873. priv->cards[i].pdev = pdev;
  874. priv->cards[i].io_cnt = 0;
  875. priv->cards[i].mem_cnt = 0;
  876. break;
  877. }
  878. }
  879. if (i == MAX_USER_CARDS) {
  880. pr_err("vgaarb: maximum user cards (%d) number reached!\n",
  881. MAX_USER_CARDS);
  882. pci_dev_put(pdev);
  883. /* XXX: which value to return? */
  884. ret_val = -ENOMEM;
  885. goto done;
  886. }
  887. ret_val = count;
  888. pci_dev_put(pdev);
  889. goto done;
  890. } else if (strncmp(curr_pos, "decodes ", 8) == 0) {
  891. curr_pos += 8;
  892. remaining -= 8;
  893. pr_devel("vgaarb: client 0x%p called 'decodes'\n", priv);
  894. if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
  895. ret_val = -EPROTO;
  896. goto done;
  897. }
  898. pdev = priv->target;
  899. if (priv->target == NULL) {
  900. ret_val = -ENODEV;
  901. goto done;
  902. }
  903. __vga_set_legacy_decoding(pdev, io_state, true);
  904. ret_val = count;
  905. goto done;
  906. }
  907. /* If we got here, the message written is not part of the protocol! */
  908. kfree(kbuf);
  909. return -EPROTO;
  910. done:
  911. kfree(kbuf);
  912. return ret_val;
  913. }
  914. static unsigned int vga_arb_fpoll(struct file *file, poll_table * wait)
  915. {
  916. struct vga_arb_private *priv = file->private_data;
  917. pr_devel("%s\n", __func__);
  918. if (priv == NULL)
  919. return -ENODEV;
  920. poll_wait(file, &vga_wait_queue, wait);
  921. return POLLIN;
  922. }
  923. static int vga_arb_open(struct inode *inode, struct file *file)
  924. {
  925. struct vga_arb_private *priv;
  926. unsigned long flags;
  927. pr_devel("%s\n", __func__);
  928. priv = kmalloc(sizeof(struct vga_arb_private), GFP_KERNEL);
  929. if (priv == NULL)
  930. return -ENOMEM;
  931. memset(priv, 0, sizeof(*priv));
  932. spin_lock_init(&priv->lock);
  933. file->private_data = priv;
  934. spin_lock_irqsave(&vga_user_lock, flags);
  935. list_add(&priv->list, &vga_user_list);
  936. spin_unlock_irqrestore(&vga_user_lock, flags);
  937. /* Set the client' lists of locks */
  938. priv->target = vga_default_device(); /* Maybe this is still null! */
  939. priv->cards[0].pdev = priv->target;
  940. priv->cards[0].io_cnt = 0;
  941. priv->cards[0].mem_cnt = 0;
  942. return 0;
  943. }
  944. static int vga_arb_release(struct inode *inode, struct file *file)
  945. {
  946. struct vga_arb_private *priv = file->private_data;
  947. struct vga_arb_user_card *uc;
  948. unsigned long flags;
  949. int i;
  950. pr_devel("%s\n", __func__);
  951. if (priv == NULL)
  952. return -ENODEV;
  953. spin_lock_irqsave(&vga_user_lock, flags);
  954. list_del(&priv->list);
  955. for (i = 0; i < MAX_USER_CARDS; i++) {
  956. uc = &priv->cards[i];
  957. if (uc->pdev == NULL)
  958. continue;
  959. pr_devel("uc->io_cnt == %d, uc->mem_cnt == %d\n",
  960. uc->io_cnt, uc->mem_cnt);
  961. while (uc->io_cnt--)
  962. vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
  963. while (uc->mem_cnt--)
  964. vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
  965. }
  966. spin_unlock_irqrestore(&vga_user_lock, flags);
  967. kfree(priv);
  968. return 0;
  969. }
  970. static void vga_arb_device_card_gone(struct pci_dev *pdev)
  971. {
  972. }
  973. /*
  974. * callback any registered clients to let them know we have a
  975. * change in VGA cards
  976. */
  977. static void vga_arbiter_notify_clients(void)
  978. {
  979. struct vga_device *vgadev;
  980. unsigned long flags;
  981. uint32_t new_decodes;
  982. bool new_state;
  983. if (!vga_arbiter_used)
  984. return;
  985. spin_lock_irqsave(&vga_lock, flags);
  986. list_for_each_entry(vgadev, &vga_list, list) {
  987. if (vga_count > 1)
  988. new_state = false;
  989. else
  990. new_state = true;
  991. if (vgadev->set_vga_decode) {
  992. new_decodes = vgadev->set_vga_decode(vgadev->cookie, new_state);
  993. vga_update_device_decodes(vgadev, new_decodes);
  994. }
  995. }
  996. spin_unlock_irqrestore(&vga_lock, flags);
  997. }
  998. static int pci_notify(struct notifier_block *nb, unsigned long action,
  999. void *data)
  1000. {
  1001. struct device *dev = data;
  1002. struct pci_dev *pdev = to_pci_dev(dev);
  1003. bool notify = false;
  1004. pr_devel("%s\n", __func__);
  1005. /* For now we're only intereted in devices added and removed. I didn't
  1006. * test this thing here, so someone needs to double check for the
  1007. * cases of hotplugable vga cards. */
  1008. if (action == BUS_NOTIFY_ADD_DEVICE)
  1009. notify = vga_arbiter_add_pci_device(pdev);
  1010. else if (action == BUS_NOTIFY_DEL_DEVICE)
  1011. notify = vga_arbiter_del_pci_device(pdev);
  1012. if (notify)
  1013. vga_arbiter_notify_clients();
  1014. return 0;
  1015. }
  1016. static struct notifier_block pci_notifier = {
  1017. .notifier_call = pci_notify,
  1018. };
  1019. static const struct file_operations vga_arb_device_fops = {
  1020. .read = vga_arb_read,
  1021. .write = vga_arb_write,
  1022. .poll = vga_arb_fpoll,
  1023. .open = vga_arb_open,
  1024. .release = vga_arb_release,
  1025. };
  1026. static struct miscdevice vga_arb_device = {
  1027. MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
  1028. };
  1029. static int __init vga_arb_device_init(void)
  1030. {
  1031. int rc;
  1032. struct pci_dev *pdev;
  1033. rc = misc_register(&vga_arb_device);
  1034. if (rc < 0)
  1035. pr_err("vgaarb: error %d registering device\n", rc);
  1036. bus_register_notifier(&pci_bus_type, &pci_notifier);
  1037. /* We add all pci devices satisfying vga class in the arbiter by
  1038. * default */
  1039. pdev = NULL;
  1040. while ((pdev =
  1041. pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  1042. PCI_ANY_ID, pdev)) != NULL)
  1043. vga_arbiter_add_pci_device(pdev);
  1044. pr_info("vgaarb: loaded\n");
  1045. return rc;
  1046. }
  1047. subsys_initcall(vga_arb_device_init);