vgaarb.c 31 KB

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