vgaarb.c 35 KB

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