vgaarb.c 36 KB

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