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 (signal_pending(current)) {
  344. rc = -EINTR;
  345. break;
  346. }
  347. schedule();
  348. remove_wait_queue(&vga_wait_queue, &wait);
  349. }
  350. return rc;
  351. }
  352. EXPORT_SYMBOL(vga_get);
  353. int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
  354. {
  355. struct vga_device *vgadev;
  356. unsigned long flags;
  357. int rc = 0;
  358. vga_check_first_use();
  359. /* The one who calls us should check for this, but lets be sure... */
  360. if (pdev == NULL)
  361. pdev = vga_default_device();
  362. if (pdev == NULL)
  363. return 0;
  364. spin_lock_irqsave(&vga_lock, flags);
  365. vgadev = vgadev_find(pdev);
  366. if (vgadev == NULL) {
  367. rc = -ENODEV;
  368. goto bail;
  369. }
  370. if (__vga_tryget(vgadev, rsrc))
  371. rc = -EBUSY;
  372. bail:
  373. spin_unlock_irqrestore(&vga_lock, flags);
  374. return rc;
  375. }
  376. EXPORT_SYMBOL(vga_tryget);
  377. void vga_put(struct pci_dev *pdev, unsigned int rsrc)
  378. {
  379. struct vga_device *vgadev;
  380. unsigned long flags;
  381. /* The one who calls us should check for this, but lets be sure... */
  382. if (pdev == NULL)
  383. pdev = vga_default_device();
  384. if (pdev == NULL)
  385. return;
  386. spin_lock_irqsave(&vga_lock, flags);
  387. vgadev = vgadev_find(pdev);
  388. if (vgadev == NULL)
  389. goto bail;
  390. __vga_put(vgadev, rsrc);
  391. bail:
  392. spin_unlock_irqrestore(&vga_lock, flags);
  393. }
  394. EXPORT_SYMBOL(vga_put);
  395. /*
  396. * Rules for using a bridge to control a VGA descendant decoding: if a bridge
  397. * has only one VGA descendant then it can be used to control the VGA routing
  398. * for that device. It should always use the bridge closest to the device to
  399. * control it. If a bridge has a direct VGA descendant, but also have a sub-
  400. * bridge VGA descendant then we cannot use that bridge to control the direct
  401. * VGA descendant. So for every device we register, we need to iterate all
  402. * its parent bridges so we can invalidate any devices using them properly.
  403. */
  404. static void vga_arbiter_check_bridge_sharing(struct vga_device *vgadev)
  405. {
  406. struct vga_device *same_bridge_vgadev;
  407. struct pci_bus *new_bus, *bus;
  408. struct pci_dev *new_bridge, *bridge;
  409. vgadev->bridge_has_one_vga = true;
  410. if (list_empty(&vga_list))
  411. return;
  412. /* okay iterate the new devices bridge hierarachy */
  413. new_bus = vgadev->pdev->bus;
  414. while (new_bus) {
  415. new_bridge = new_bus->self;
  416. /* go through list of devices already registered */
  417. list_for_each_entry(same_bridge_vgadev, &vga_list, list) {
  418. bus = same_bridge_vgadev->pdev->bus;
  419. bridge = bus->self;
  420. /* see if the share a bridge with this device */
  421. if (new_bridge == bridge) {
  422. /*
  423. * If their direct parent bridge is the same
  424. * as any bridge of this device then it can't
  425. * be used for that device.
  426. */
  427. same_bridge_vgadev->bridge_has_one_vga = false;
  428. }
  429. /*
  430. * Now iterate the previous devices bridge hierarchy.
  431. * If the new devices parent bridge is in the other
  432. * devices hierarchy then we can't use it to control
  433. * this device
  434. */
  435. while (bus) {
  436. bridge = bus->self;
  437. if (bridge && bridge == vgadev->pdev->bus->self)
  438. vgadev->bridge_has_one_vga = false;
  439. bus = bus->parent;
  440. }
  441. }
  442. new_bus = new_bus->parent;
  443. }
  444. }
  445. /*
  446. * Currently, we assume that the "initial" setup of the system is
  447. * not sane, that is we come up with conflicting devices and let
  448. * the arbiter's client decides if devices decodes or not legacy
  449. * things.
  450. */
  451. static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
  452. {
  453. struct vga_device *vgadev;
  454. unsigned long flags;
  455. struct pci_bus *bus;
  456. struct pci_dev *bridge;
  457. u16 cmd;
  458. /* Only deal with VGA class devices */
  459. if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  460. return false;
  461. /* Allocate structure */
  462. vgadev = kmalloc(sizeof(struct vga_device), GFP_KERNEL);
  463. if (vgadev == NULL) {
  464. pr_err("failed to allocate pci device\n");
  465. /*
  466. * What to do on allocation failure ? For now, let's just do
  467. * nothing, I'm not sure there is anything saner to be done.
  468. */
  469. return false;
  470. }
  471. memset(vgadev, 0, sizeof(*vgadev));
  472. /* Take lock & check for duplicates */
  473. spin_lock_irqsave(&vga_lock, flags);
  474. if (vgadev_find(pdev) != NULL) {
  475. BUG_ON(1);
  476. goto fail;
  477. }
  478. vgadev->pdev = pdev;
  479. /* By default, assume we decode everything */
  480. vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  481. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  482. /* by default mark it as decoding */
  483. vga_decode_count++;
  484. /* Mark that we "own" resources based on our enables, we will
  485. * clear that below if the bridge isn't forwarding
  486. */
  487. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  488. if (cmd & PCI_COMMAND_IO)
  489. vgadev->owns |= VGA_RSRC_LEGACY_IO;
  490. if (cmd & PCI_COMMAND_MEMORY)
  491. vgadev->owns |= VGA_RSRC_LEGACY_MEM;
  492. /* Check if VGA cycles can get down to us */
  493. bus = pdev->bus;
  494. while (bus) {
  495. bridge = bus->self;
  496. if (bridge) {
  497. u16 l;
  498. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &l);
  499. if (!(l & PCI_BRIDGE_CTL_VGA)) {
  500. vgadev->owns = 0;
  501. break;
  502. }
  503. }
  504. bus = bus->parent;
  505. }
  506. /* Deal with VGA default device. Use first enabled one
  507. * by default if arch doesn't have it's own hook
  508. */
  509. if (vga_default == NULL &&
  510. ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)) {
  511. pr_info("setting as boot device: PCI:%s\n", pci_name(pdev));
  512. vga_set_default_device(pdev);
  513. }
  514. vga_arbiter_check_bridge_sharing(vgadev);
  515. /* Add to the list */
  516. list_add(&vgadev->list, &vga_list);
  517. vga_count++;
  518. pr_info("device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
  519. pci_name(pdev),
  520. vga_iostate_to_str(vgadev->decodes),
  521. vga_iostate_to_str(vgadev->owns),
  522. vga_iostate_to_str(vgadev->locks));
  523. spin_unlock_irqrestore(&vga_lock, flags);
  524. return true;
  525. fail:
  526. spin_unlock_irqrestore(&vga_lock, flags);
  527. kfree(vgadev);
  528. return false;
  529. }
  530. static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
  531. {
  532. struct vga_device *vgadev;
  533. unsigned long flags;
  534. bool ret = true;
  535. spin_lock_irqsave(&vga_lock, flags);
  536. vgadev = vgadev_find(pdev);
  537. if (vgadev == NULL) {
  538. ret = false;
  539. goto bail;
  540. }
  541. if (vga_default == pdev)
  542. vga_set_default_device(NULL);
  543. if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
  544. vga_decode_count--;
  545. /* Remove entry from list */
  546. list_del(&vgadev->list);
  547. vga_count--;
  548. /* Notify userland driver that the device is gone so it discards
  549. * it's copies of the pci_dev pointer
  550. */
  551. vga_arb_device_card_gone(pdev);
  552. /* Wake up all possible waiters */
  553. wake_up_all(&vga_wait_queue);
  554. bail:
  555. spin_unlock_irqrestore(&vga_lock, flags);
  556. kfree(vgadev);
  557. return ret;
  558. }
  559. /* this is called with the lock */
  560. static inline void vga_update_device_decodes(struct vga_device *vgadev,
  561. int new_decodes)
  562. {
  563. int old_decodes, decodes_removed, decodes_unlocked;
  564. old_decodes = vgadev->decodes;
  565. decodes_removed = ~new_decodes & old_decodes;
  566. decodes_unlocked = vgadev->locks & decodes_removed;
  567. vgadev->decodes = new_decodes;
  568. pr_info("device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
  569. pci_name(vgadev->pdev),
  570. vga_iostate_to_str(old_decodes),
  571. vga_iostate_to_str(vgadev->decodes),
  572. vga_iostate_to_str(vgadev->owns));
  573. /* if we removed locked decodes, lock count goes to zero, and release */
  574. if (decodes_unlocked) {
  575. if (decodes_unlocked & VGA_RSRC_LEGACY_IO)
  576. vgadev->io_lock_cnt = 0;
  577. if (decodes_unlocked & VGA_RSRC_LEGACY_MEM)
  578. vgadev->mem_lock_cnt = 0;
  579. __vga_put(vgadev, decodes_unlocked);
  580. }
  581. /* change decodes counter */
  582. if (old_decodes & VGA_RSRC_LEGACY_MASK &&
  583. !(new_decodes & VGA_RSRC_LEGACY_MASK))
  584. vga_decode_count--;
  585. if (!(old_decodes & VGA_RSRC_LEGACY_MASK) &&
  586. new_decodes & VGA_RSRC_LEGACY_MASK)
  587. vga_decode_count++;
  588. pr_debug("decoding count now is: %d\n", vga_decode_count);
  589. }
  590. static void __vga_set_legacy_decoding(struct pci_dev *pdev,
  591. unsigned int decodes,
  592. 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,
  622. bool decode))
  623. {
  624. int ret = -ENODEV;
  625. struct vga_device *vgadev;
  626. unsigned long flags;
  627. spin_lock_irqsave(&vga_lock, flags);
  628. vgadev = vgadev_find(pdev);
  629. if (!vgadev)
  630. goto bail;
  631. vgadev->irq_set_state = irq_set_state;
  632. vgadev->set_vga_decode = set_vga_decode;
  633. vgadev->cookie = cookie;
  634. ret = 0;
  635. bail:
  636. spin_unlock_irqrestore(&vga_lock, flags);
  637. return ret;
  638. }
  639. EXPORT_SYMBOL(vga_client_register);
  640. /*
  641. * Char driver implementation
  642. *
  643. * Semantics is:
  644. *
  645. * open : open user instance of the arbitrer. by default, it's
  646. * attached to the default VGA device of the system.
  647. *
  648. * close : close user instance, release locks
  649. *
  650. * read : return a string indicating the status of the target.
  651. * an IO state string is of the form {io,mem,io+mem,none},
  652. * mc and ic are respectively mem and io lock counts (for
  653. * debugging/diagnostic only). "decodes" indicate what the
  654. * card currently decodes, "owns" indicates what is currently
  655. * enabled on it, and "locks" indicates what is locked by this
  656. * card. If the card is unplugged, we get "invalid" then for
  657. * card_ID and an -ENODEV error is returned for any command
  658. * until a new card is targeted
  659. *
  660. * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
  661. *
  662. * write : write a command to the arbiter. List of commands is:
  663. *
  664. * target <card_ID> : switch target to card <card_ID> (see below)
  665. * lock <io_state> : acquires locks on target ("none" is invalid io_state)
  666. * trylock <io_state> : non-blocking acquire locks on target
  667. * unlock <io_state> : release locks on target
  668. * unlock all : release all locks on target held by this user
  669. * decodes <io_state> : set the legacy decoding attributes for the card
  670. *
  671. * poll : event if something change on any card (not just the target)
  672. *
  673. * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
  674. * to go back to the system default card (TODO: not implemented yet).
  675. * Currently, only PCI is supported as a prefix, but the userland API may
  676. * support other bus types in the future, even if the current kernel
  677. * implementation doesn't.
  678. *
  679. * Note about locks:
  680. *
  681. * The driver keeps track of which user has what locks on which card. It
  682. * supports stacking, like the kernel one. This complexifies the implementation
  683. * a bit, but makes the arbiter more tolerant to userspace problems and able
  684. * to properly cleanup in all cases when a process dies.
  685. * Currently, a max of 16 cards simultaneously can have locks issued from
  686. * userspace for a given user (file descriptor instance) of the arbiter.
  687. *
  688. * If the device is hot-unplugged, there is a hook inside the module to notify
  689. * they being added/removed in the system and automatically added/removed in
  690. * the arbiter.
  691. */
  692. #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
  693. #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
  694. /*
  695. * Each user has an array of these, tracking which cards have locks
  696. */
  697. struct vga_arb_user_card {
  698. struct pci_dev *pdev;
  699. unsigned int mem_cnt;
  700. unsigned int io_cnt;
  701. };
  702. struct vga_arb_private {
  703. struct list_head list;
  704. struct pci_dev *target;
  705. struct vga_arb_user_card cards[MAX_USER_CARDS];
  706. spinlock_t lock;
  707. };
  708. static LIST_HEAD(vga_user_list);
  709. static DEFINE_SPINLOCK(vga_user_lock);
  710. /*
  711. * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
  712. * returns the respective values. If the string is not in this format,
  713. * it returns 0.
  714. */
  715. static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
  716. unsigned int *bus, unsigned int *devfn)
  717. {
  718. int n;
  719. unsigned int slot, func;
  720. n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
  721. if (n != 4)
  722. return 0;
  723. *devfn = PCI_DEVFN(slot, func);
  724. return 1;
  725. }
  726. static ssize_t vga_arb_read(struct file *file, char __user *buf,
  727. size_t count, loff_t *ppos)
  728. {
  729. struct vga_arb_private *priv = file->private_data;
  730. struct vga_device *vgadev;
  731. struct pci_dev *pdev;
  732. unsigned long flags;
  733. size_t len;
  734. int rc;
  735. char *lbuf;
  736. lbuf = kmalloc(1024, GFP_KERNEL);
  737. if (lbuf == NULL)
  738. return -ENOMEM;
  739. /* Shields against vga_arb_device_card_gone (pci_dev going
  740. * away), and allows access to vga list
  741. */
  742. spin_lock_irqsave(&vga_lock, flags);
  743. /* If we are targeting the default, use it */
  744. pdev = priv->target;
  745. if (pdev == NULL || pdev == PCI_INVALID_CARD) {
  746. spin_unlock_irqrestore(&vga_lock, flags);
  747. len = sprintf(lbuf, "invalid");
  748. goto done;
  749. }
  750. /* Find card vgadev structure */
  751. vgadev = vgadev_find(pdev);
  752. if (vgadev == NULL) {
  753. /* Wow, it's not in the list, that shouldn't happen,
  754. * let's fix us up and return invalid card
  755. */
  756. if (pdev == priv->target)
  757. vga_arb_device_card_gone(pdev);
  758. spin_unlock_irqrestore(&vga_lock, flags);
  759. len = sprintf(lbuf, "invalid");
  760. goto done;
  761. }
  762. /* Fill the buffer with infos */
  763. len = snprintf(lbuf, 1024,
  764. "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
  765. vga_decode_count, pci_name(pdev),
  766. vga_iostate_to_str(vgadev->decodes),
  767. vga_iostate_to_str(vgadev->owns),
  768. vga_iostate_to_str(vgadev->locks),
  769. vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
  770. spin_unlock_irqrestore(&vga_lock, flags);
  771. done:
  772. /* Copy that to user */
  773. if (len > count)
  774. len = count;
  775. rc = copy_to_user(buf, lbuf, len);
  776. kfree(lbuf);
  777. if (rc)
  778. return -EFAULT;
  779. return len;
  780. }
  781. /*
  782. * TODO: To avoid parsing inside kernel and to improve the speed we may
  783. * consider use ioctl here
  784. */
  785. static ssize_t vga_arb_write(struct file *file, const char __user *buf,
  786. size_t count, loff_t *ppos)
  787. {
  788. struct vga_arb_private *priv = file->private_data;
  789. struct vga_arb_user_card *uc = NULL;
  790. struct pci_dev *pdev;
  791. unsigned int io_state;
  792. char *kbuf, *curr_pos;
  793. size_t remaining = count;
  794. int ret_val;
  795. int i;
  796. kbuf = kmalloc(count + 1, GFP_KERNEL);
  797. if (!kbuf)
  798. return -ENOMEM;
  799. if (copy_from_user(kbuf, buf, count)) {
  800. kfree(kbuf);
  801. return -EFAULT;
  802. }
  803. curr_pos = kbuf;
  804. kbuf[count] = '\0'; /* Just to make sure... */
  805. if (strncmp(curr_pos, "lock ", 5) == 0) {
  806. curr_pos += 5;
  807. remaining -= 5;
  808. pr_debug("client 0x%p called 'lock'\n", priv);
  809. if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
  810. ret_val = -EPROTO;
  811. goto done;
  812. }
  813. if (io_state == VGA_RSRC_NONE) {
  814. ret_val = -EPROTO;
  815. goto done;
  816. }
  817. pdev = priv->target;
  818. if (priv->target == NULL) {
  819. ret_val = -ENODEV;
  820. goto done;
  821. }
  822. vga_get_uninterruptible(pdev, io_state);
  823. /* Update the client's locks lists... */
  824. for (i = 0; i < MAX_USER_CARDS; i++) {
  825. if (priv->cards[i].pdev == pdev) {
  826. if (io_state & VGA_RSRC_LEGACY_IO)
  827. priv->cards[i].io_cnt++;
  828. if (io_state & VGA_RSRC_LEGACY_MEM)
  829. priv->cards[i].mem_cnt++;
  830. break;
  831. }
  832. }
  833. ret_val = count;
  834. goto done;
  835. } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
  836. curr_pos += 7;
  837. remaining -= 7;
  838. pr_debug("client 0x%p called 'unlock'\n", priv);
  839. if (strncmp(curr_pos, "all", 3) == 0)
  840. io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  841. else {
  842. if (!vga_str_to_iostate
  843. (curr_pos, remaining, &io_state)) {
  844. ret_val = -EPROTO;
  845. goto done;
  846. }
  847. /* TODO: Add this?
  848. if (io_state == VGA_RSRC_NONE) {
  849. ret_val = -EPROTO;
  850. goto done;
  851. }
  852. */
  853. }
  854. pdev = priv->target;
  855. if (priv->target == NULL) {
  856. ret_val = -ENODEV;
  857. goto done;
  858. }
  859. for (i = 0; i < MAX_USER_CARDS; i++) {
  860. if (priv->cards[i].pdev == pdev)
  861. uc = &priv->cards[i];
  862. }
  863. if (!uc) {
  864. ret_val = -EINVAL;
  865. goto done;
  866. }
  867. if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
  868. ret_val = -EINVAL;
  869. goto done;
  870. }
  871. if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
  872. ret_val = -EINVAL;
  873. goto done;
  874. }
  875. vga_put(pdev, io_state);
  876. if (io_state & VGA_RSRC_LEGACY_IO)
  877. uc->io_cnt--;
  878. if (io_state & VGA_RSRC_LEGACY_MEM)
  879. uc->mem_cnt--;
  880. ret_val = count;
  881. goto done;
  882. } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
  883. curr_pos += 8;
  884. remaining -= 8;
  885. pr_debug("client 0x%p called 'trylock'\n", priv);
  886. if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
  887. ret_val = -EPROTO;
  888. goto done;
  889. }
  890. /* TODO: Add this?
  891. if (io_state == VGA_RSRC_NONE) {
  892. ret_val = -EPROTO;
  893. goto done;
  894. }
  895. */
  896. pdev = priv->target;
  897. if (priv->target == NULL) {
  898. ret_val = -ENODEV;
  899. goto done;
  900. }
  901. if (vga_tryget(pdev, io_state)) {
  902. /* Update the client's locks lists... */
  903. for (i = 0; i < MAX_USER_CARDS; i++) {
  904. if (priv->cards[i].pdev == pdev) {
  905. if (io_state & VGA_RSRC_LEGACY_IO)
  906. priv->cards[i].io_cnt++;
  907. if (io_state & VGA_RSRC_LEGACY_MEM)
  908. priv->cards[i].mem_cnt++;
  909. break;
  910. }
  911. }
  912. ret_val = count;
  913. goto done;
  914. } else {
  915. ret_val = -EBUSY;
  916. goto done;
  917. }
  918. } else if (strncmp(curr_pos, "target ", 7) == 0) {
  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("%s ==> %x:%x:%x.%x\n", curr_pos,
  934. domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
  935. pdev = pci_get_domain_bus_and_slot(domain, bus, devfn);
  936. pr_debug("pdev %p\n", pdev);
  937. if (!pdev) {
  938. pr_err("invalid PCI address %x:%x:%x\n",
  939. domain, bus, devfn);
  940. ret_val = -ENODEV;
  941. goto done;
  942. }
  943. }
  944. vgadev = vgadev_find(pdev);
  945. pr_debug("vgadev %p\n", vgadev);
  946. if (vgadev == NULL) {
  947. if (pdev) {
  948. pr_err("this pci device is not a vga device\n");
  949. pci_dev_put(pdev);
  950. }
  951. ret_val = -ENODEV;
  952. goto done;
  953. }
  954. priv->target = pdev;
  955. for (i = 0; i < MAX_USER_CARDS; i++) {
  956. if (priv->cards[i].pdev == pdev)
  957. break;
  958. if (priv->cards[i].pdev == NULL) {
  959. priv->cards[i].pdev = pdev;
  960. priv->cards[i].io_cnt = 0;
  961. priv->cards[i].mem_cnt = 0;
  962. break;
  963. }
  964. }
  965. if (i == MAX_USER_CARDS) {
  966. pr_err("maximum user cards (%d) number reached!\n",
  967. MAX_USER_CARDS);
  968. pci_dev_put(pdev);
  969. /* XXX: which value to return? */
  970. ret_val = -ENOMEM;
  971. goto done;
  972. }
  973. ret_val = count;
  974. pci_dev_put(pdev);
  975. goto done;
  976. } else if (strncmp(curr_pos, "decodes ", 8) == 0) {
  977. curr_pos += 8;
  978. remaining -= 8;
  979. pr_debug("client 0x%p called 'decodes'\n", priv);
  980. if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
  981. ret_val = -EPROTO;
  982. goto done;
  983. }
  984. pdev = priv->target;
  985. if (priv->target == NULL) {
  986. ret_val = -ENODEV;
  987. goto done;
  988. }
  989. __vga_set_legacy_decoding(pdev, io_state, true);
  990. ret_val = count;
  991. goto done;
  992. }
  993. /* If we got here, the message written is not part of the protocol! */
  994. kfree(kbuf);
  995. return -EPROTO;
  996. done:
  997. kfree(kbuf);
  998. return ret_val;
  999. }
  1000. static unsigned int vga_arb_fpoll(struct file *file, poll_table *wait)
  1001. {
  1002. struct vga_arb_private *priv = file->private_data;
  1003. pr_debug("%s\n", __func__);
  1004. if (priv == NULL)
  1005. return -ENODEV;
  1006. poll_wait(file, &vga_wait_queue, wait);
  1007. return POLLIN;
  1008. }
  1009. static int vga_arb_open(struct inode *inode, struct file *file)
  1010. {
  1011. struct vga_arb_private *priv;
  1012. unsigned long flags;
  1013. pr_debug("%s\n", __func__);
  1014. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1015. if (priv == NULL)
  1016. return -ENOMEM;
  1017. spin_lock_init(&priv->lock);
  1018. file->private_data = priv;
  1019. spin_lock_irqsave(&vga_user_lock, flags);
  1020. list_add(&priv->list, &vga_user_list);
  1021. spin_unlock_irqrestore(&vga_user_lock, flags);
  1022. /* Set the client' lists of locks */
  1023. priv->target = vga_default_device(); /* Maybe this is still null! */
  1024. priv->cards[0].pdev = priv->target;
  1025. priv->cards[0].io_cnt = 0;
  1026. priv->cards[0].mem_cnt = 0;
  1027. return 0;
  1028. }
  1029. static int vga_arb_release(struct inode *inode, struct file *file)
  1030. {
  1031. struct vga_arb_private *priv = file->private_data;
  1032. struct vga_arb_user_card *uc;
  1033. unsigned long flags;
  1034. int i;
  1035. pr_debug("%s\n", __func__);
  1036. if (priv == NULL)
  1037. return -ENODEV;
  1038. spin_lock_irqsave(&vga_user_lock, flags);
  1039. list_del(&priv->list);
  1040. for (i = 0; i < MAX_USER_CARDS; i++) {
  1041. uc = &priv->cards[i];
  1042. if (uc->pdev == NULL)
  1043. continue;
  1044. pr_debug("uc->io_cnt == %d, uc->mem_cnt == %d\n",
  1045. uc->io_cnt, uc->mem_cnt);
  1046. while (uc->io_cnt--)
  1047. vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
  1048. while (uc->mem_cnt--)
  1049. vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
  1050. }
  1051. spin_unlock_irqrestore(&vga_user_lock, flags);
  1052. kfree(priv);
  1053. return 0;
  1054. }
  1055. static void vga_arb_device_card_gone(struct pci_dev *pdev)
  1056. {
  1057. }
  1058. /*
  1059. * callback any registered clients to let them know we have a
  1060. * change in VGA cards
  1061. */
  1062. static void vga_arbiter_notify_clients(void)
  1063. {
  1064. struct vga_device *vgadev;
  1065. unsigned long flags;
  1066. uint32_t new_decodes;
  1067. bool new_state;
  1068. if (!vga_arbiter_used)
  1069. return;
  1070. spin_lock_irqsave(&vga_lock, flags);
  1071. list_for_each_entry(vgadev, &vga_list, list) {
  1072. if (vga_count > 1)
  1073. new_state = false;
  1074. else
  1075. new_state = true;
  1076. if (vgadev->set_vga_decode) {
  1077. new_decodes = vgadev->set_vga_decode(vgadev->cookie,
  1078. new_state);
  1079. vga_update_device_decodes(vgadev, new_decodes);
  1080. }
  1081. }
  1082. spin_unlock_irqrestore(&vga_lock, flags);
  1083. }
  1084. static int pci_notify(struct notifier_block *nb, unsigned long action,
  1085. void *data)
  1086. {
  1087. struct device *dev = data;
  1088. struct pci_dev *pdev = to_pci_dev(dev);
  1089. bool notify = false;
  1090. pr_debug("%s\n", __func__);
  1091. /* For now we're only intereted in devices added and removed. I didn't
  1092. * test this thing here, so someone needs to double check for the
  1093. * cases of hotplugable vga cards. */
  1094. if (action == BUS_NOTIFY_ADD_DEVICE)
  1095. notify = vga_arbiter_add_pci_device(pdev);
  1096. else if (action == BUS_NOTIFY_DEL_DEVICE)
  1097. notify = vga_arbiter_del_pci_device(pdev);
  1098. if (notify)
  1099. vga_arbiter_notify_clients();
  1100. return 0;
  1101. }
  1102. static struct notifier_block pci_notifier = {
  1103. .notifier_call = pci_notify,
  1104. };
  1105. static const struct file_operations vga_arb_device_fops = {
  1106. .read = vga_arb_read,
  1107. .write = vga_arb_write,
  1108. .poll = vga_arb_fpoll,
  1109. .open = vga_arb_open,
  1110. .release = vga_arb_release,
  1111. .llseek = noop_llseek,
  1112. };
  1113. static struct miscdevice vga_arb_device = {
  1114. MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
  1115. };
  1116. static int __init vga_arb_device_init(void)
  1117. {
  1118. int rc;
  1119. struct pci_dev *pdev;
  1120. struct vga_device *vgadev;
  1121. rc = misc_register(&vga_arb_device);
  1122. if (rc < 0)
  1123. pr_err("error %d registering device\n", rc);
  1124. bus_register_notifier(&pci_bus_type, &pci_notifier);
  1125. /* We add all pci devices satisfying vga class in the arbiter by
  1126. * default */
  1127. pdev = NULL;
  1128. while ((pdev =
  1129. pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  1130. PCI_ANY_ID, pdev)) != NULL)
  1131. vga_arbiter_add_pci_device(pdev);
  1132. pr_info("loaded\n");
  1133. list_for_each_entry(vgadev, &vga_list, list) {
  1134. #if defined(CONFIG_X86) || defined(CONFIG_IA64)
  1135. /*
  1136. * Override vga_arbiter_add_pci_device()'s I/O based detection
  1137. * as it may take the wrong device (e.g. on Apple system under
  1138. * EFI).
  1139. *
  1140. * Select the device owning the boot framebuffer if there is
  1141. * one.
  1142. */
  1143. resource_size_t start, end, limit;
  1144. unsigned long flags;
  1145. int i;
  1146. limit = screen_info.lfb_base + screen_info.lfb_size;
  1147. /* Does firmware framebuffer belong to us? */
  1148. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1149. flags = pci_resource_flags(vgadev->pdev, i);
  1150. if ((flags & IORESOURCE_MEM) == 0)
  1151. continue;
  1152. start = pci_resource_start(vgadev->pdev, i);
  1153. end = pci_resource_end(vgadev->pdev, i);
  1154. if (!start || !end)
  1155. continue;
  1156. if (screen_info.lfb_base < start || limit >= end)
  1157. continue;
  1158. if (!vga_default_device())
  1159. pr_info("setting as boot device: PCI:%s\n",
  1160. pci_name(vgadev->pdev));
  1161. else if (vgadev->pdev != vga_default_device())
  1162. pr_info("overriding boot device: PCI:%s\n",
  1163. pci_name(vgadev->pdev));
  1164. vga_set_default_device(vgadev->pdev);
  1165. }
  1166. #endif
  1167. if (vgadev->bridge_has_one_vga)
  1168. pr_info("bridge control possible %s\n",
  1169. pci_name(vgadev->pdev));
  1170. else
  1171. pr_info("no bridge control possible %s\n",
  1172. pci_name(vgadev->pdev));
  1173. }
  1174. return rc;
  1175. }
  1176. subsys_initcall(vga_arb_device_init);