vio.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. /*
  2. * IBM PowerPC Virtual I/O Infrastructure Support.
  3. *
  4. * Copyright (c) 2003,2008 IBM Corp.
  5. * Dave Engebretsen engebret@us.ibm.com
  6. * Santiago Leon santil@us.ibm.com
  7. * Hollis Blanchard <hollisb@us.ibm.com>
  8. * Stephen Rothwell
  9. * Robert Jennings <rcjenn@us.ibm.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/device.h>
  18. #include <linux/init.h>
  19. #include <linux/console.h>
  20. #include <linux/module.h>
  21. #include <linux/mm.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/kobject.h>
  24. #include <asm/iommu.h>
  25. #include <asm/dma.h>
  26. #include <asm/vio.h>
  27. #include <asm/prom.h>
  28. #include <asm/firmware.h>
  29. #include <asm/tce.h>
  30. #include <asm/abs_addr.h>
  31. #include <asm/page.h>
  32. #include <asm/hvcall.h>
  33. #include <asm/iseries/vio.h>
  34. #include <asm/iseries/hv_types.h>
  35. #include <asm/iseries/hv_lp_config.h>
  36. #include <asm/iseries/hv_call_xm.h>
  37. #include <asm/iseries/iommu.h>
  38. static struct bus_type vio_bus_type;
  39. static struct vio_dev vio_bus_device = { /* fake "parent" device */
  40. .name = "vio",
  41. .type = "",
  42. .dev.init_name = "vio",
  43. .dev.bus = &vio_bus_type,
  44. };
  45. #ifdef CONFIG_PPC_SMLPAR
  46. /**
  47. * vio_cmo_pool - A pool of IO memory for CMO use
  48. *
  49. * @size: The size of the pool in bytes
  50. * @free: The amount of free memory in the pool
  51. */
  52. struct vio_cmo_pool {
  53. size_t size;
  54. size_t free;
  55. };
  56. /* How many ms to delay queued balance work */
  57. #define VIO_CMO_BALANCE_DELAY 100
  58. /* Portion out IO memory to CMO devices by this chunk size */
  59. #define VIO_CMO_BALANCE_CHUNK 131072
  60. /**
  61. * vio_cmo_dev_entry - A device that is CMO-enabled and requires entitlement
  62. *
  63. * @vio_dev: struct vio_dev pointer
  64. * @list: pointer to other devices on bus that are being tracked
  65. */
  66. struct vio_cmo_dev_entry {
  67. struct vio_dev *viodev;
  68. struct list_head list;
  69. };
  70. /**
  71. * vio_cmo - VIO bus accounting structure for CMO entitlement
  72. *
  73. * @lock: spinlock for entire structure
  74. * @balance_q: work queue for balancing system entitlement
  75. * @device_list: list of CMO-enabled devices requiring entitlement
  76. * @entitled: total system entitlement in bytes
  77. * @reserve: pool of memory from which devices reserve entitlement, incl. spare
  78. * @excess: pool of excess entitlement not needed for device reserves or spare
  79. * @spare: IO memory for device hotplug functionality
  80. * @min: minimum necessary for system operation
  81. * @desired: desired memory for system operation
  82. * @curr: bytes currently allocated
  83. * @high: high water mark for IO data usage
  84. */
  85. struct vio_cmo {
  86. spinlock_t lock;
  87. struct delayed_work balance_q;
  88. struct list_head device_list;
  89. size_t entitled;
  90. struct vio_cmo_pool reserve;
  91. struct vio_cmo_pool excess;
  92. size_t spare;
  93. size_t min;
  94. size_t desired;
  95. size_t curr;
  96. size_t high;
  97. } vio_cmo;
  98. /**
  99. * vio_cmo_OF_devices - Count the number of OF devices that have DMA windows
  100. */
  101. static int vio_cmo_num_OF_devs(void)
  102. {
  103. struct device_node *node_vroot;
  104. int count = 0;
  105. /*
  106. * Count the number of vdevice entries with an
  107. * ibm,my-dma-window OF property
  108. */
  109. node_vroot = of_find_node_by_name(NULL, "vdevice");
  110. if (node_vroot) {
  111. struct device_node *of_node;
  112. struct property *prop;
  113. for_each_child_of_node(node_vroot, of_node) {
  114. prop = of_find_property(of_node, "ibm,my-dma-window",
  115. NULL);
  116. if (prop)
  117. count++;
  118. }
  119. }
  120. of_node_put(node_vroot);
  121. return count;
  122. }
  123. /**
  124. * vio_cmo_alloc - allocate IO memory for CMO-enable devices
  125. *
  126. * @viodev: VIO device requesting IO memory
  127. * @size: size of allocation requested
  128. *
  129. * Allocations come from memory reserved for the devices and any excess
  130. * IO memory available to all devices. The spare pool used to service
  131. * hotplug must be equal to %VIO_CMO_MIN_ENT for the excess pool to be
  132. * made available.
  133. *
  134. * Return codes:
  135. * 0 for successful allocation and -ENOMEM for a failure
  136. */
  137. static inline int vio_cmo_alloc(struct vio_dev *viodev, size_t size)
  138. {
  139. unsigned long flags;
  140. size_t reserve_free = 0;
  141. size_t excess_free = 0;
  142. int ret = -ENOMEM;
  143. spin_lock_irqsave(&vio_cmo.lock, flags);
  144. /* Determine the amount of free entitlement available in reserve */
  145. if (viodev->cmo.entitled > viodev->cmo.allocated)
  146. reserve_free = viodev->cmo.entitled - viodev->cmo.allocated;
  147. /* If spare is not fulfilled, the excess pool can not be used. */
  148. if (vio_cmo.spare >= VIO_CMO_MIN_ENT)
  149. excess_free = vio_cmo.excess.free;
  150. /* The request can be satisfied */
  151. if ((reserve_free + excess_free) >= size) {
  152. vio_cmo.curr += size;
  153. if (vio_cmo.curr > vio_cmo.high)
  154. vio_cmo.high = vio_cmo.curr;
  155. viodev->cmo.allocated += size;
  156. size -= min(reserve_free, size);
  157. vio_cmo.excess.free -= size;
  158. ret = 0;
  159. }
  160. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  161. return ret;
  162. }
  163. /**
  164. * vio_cmo_dealloc - deallocate IO memory from CMO-enable devices
  165. * @viodev: VIO device freeing IO memory
  166. * @size: size of deallocation
  167. *
  168. * IO memory is freed by the device back to the correct memory pools.
  169. * The spare pool is replenished first from either memory pool, then
  170. * the reserve pool is used to reduce device entitlement, the excess
  171. * pool is used to increase the reserve pool toward the desired entitlement
  172. * target, and then the remaining memory is returned to the pools.
  173. *
  174. */
  175. static inline void vio_cmo_dealloc(struct vio_dev *viodev, size_t size)
  176. {
  177. unsigned long flags;
  178. size_t spare_needed = 0;
  179. size_t excess_freed = 0;
  180. size_t reserve_freed = size;
  181. size_t tmp;
  182. int balance = 0;
  183. spin_lock_irqsave(&vio_cmo.lock, flags);
  184. vio_cmo.curr -= size;
  185. /* Amount of memory freed from the excess pool */
  186. if (viodev->cmo.allocated > viodev->cmo.entitled) {
  187. excess_freed = min(reserve_freed, (viodev->cmo.allocated -
  188. viodev->cmo.entitled));
  189. reserve_freed -= excess_freed;
  190. }
  191. /* Remove allocation from device */
  192. viodev->cmo.allocated -= (reserve_freed + excess_freed);
  193. /* Spare is a subset of the reserve pool, replenish it first. */
  194. spare_needed = VIO_CMO_MIN_ENT - vio_cmo.spare;
  195. /*
  196. * Replenish the spare in the reserve pool from the excess pool.
  197. * This moves entitlement into the reserve pool.
  198. */
  199. if (spare_needed && excess_freed) {
  200. tmp = min(excess_freed, spare_needed);
  201. vio_cmo.excess.size -= tmp;
  202. vio_cmo.reserve.size += tmp;
  203. vio_cmo.spare += tmp;
  204. excess_freed -= tmp;
  205. spare_needed -= tmp;
  206. balance = 1;
  207. }
  208. /*
  209. * Replenish the spare in the reserve pool from the reserve pool.
  210. * This removes entitlement from the device down to VIO_CMO_MIN_ENT,
  211. * if needed, and gives it to the spare pool. The amount of used
  212. * memory in this pool does not change.
  213. */
  214. if (spare_needed && reserve_freed) {
  215. tmp = min(spare_needed, min(reserve_freed,
  216. (viodev->cmo.entitled -
  217. VIO_CMO_MIN_ENT)));
  218. vio_cmo.spare += tmp;
  219. viodev->cmo.entitled -= tmp;
  220. reserve_freed -= tmp;
  221. spare_needed -= tmp;
  222. balance = 1;
  223. }
  224. /*
  225. * Increase the reserve pool until the desired allocation is met.
  226. * Move an allocation freed from the excess pool into the reserve
  227. * pool and schedule a balance operation.
  228. */
  229. if (excess_freed && (vio_cmo.desired > vio_cmo.reserve.size)) {
  230. tmp = min(excess_freed, (vio_cmo.desired - vio_cmo.reserve.size));
  231. vio_cmo.excess.size -= tmp;
  232. vio_cmo.reserve.size += tmp;
  233. excess_freed -= tmp;
  234. balance = 1;
  235. }
  236. /* Return memory from the excess pool to that pool */
  237. if (excess_freed)
  238. vio_cmo.excess.free += excess_freed;
  239. if (balance)
  240. schedule_delayed_work(&vio_cmo.balance_q, VIO_CMO_BALANCE_DELAY);
  241. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  242. }
  243. /**
  244. * vio_cmo_entitlement_update - Manage system entitlement changes
  245. *
  246. * @new_entitlement: new system entitlement to attempt to accommodate
  247. *
  248. * Increases in entitlement will be used to fulfill the spare entitlement
  249. * and the rest is given to the excess pool. Decreases, if they are
  250. * possible, come from the excess pool and from unused device entitlement
  251. *
  252. * Returns: 0 on success, -ENOMEM when change can not be made
  253. */
  254. int vio_cmo_entitlement_update(size_t new_entitlement)
  255. {
  256. struct vio_dev *viodev;
  257. struct vio_cmo_dev_entry *dev_ent;
  258. unsigned long flags;
  259. size_t avail, delta, tmp;
  260. spin_lock_irqsave(&vio_cmo.lock, flags);
  261. /* Entitlement increases */
  262. if (new_entitlement > vio_cmo.entitled) {
  263. delta = new_entitlement - vio_cmo.entitled;
  264. /* Fulfill spare allocation */
  265. if (vio_cmo.spare < VIO_CMO_MIN_ENT) {
  266. tmp = min(delta, (VIO_CMO_MIN_ENT - vio_cmo.spare));
  267. vio_cmo.spare += tmp;
  268. vio_cmo.reserve.size += tmp;
  269. delta -= tmp;
  270. }
  271. /* Remaining new allocation goes to the excess pool */
  272. vio_cmo.entitled += delta;
  273. vio_cmo.excess.size += delta;
  274. vio_cmo.excess.free += delta;
  275. goto out;
  276. }
  277. /* Entitlement decreases */
  278. delta = vio_cmo.entitled - new_entitlement;
  279. avail = vio_cmo.excess.free;
  280. /*
  281. * Need to check how much unused entitlement each device can
  282. * sacrifice to fulfill entitlement change.
  283. */
  284. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  285. if (avail >= delta)
  286. break;
  287. viodev = dev_ent->viodev;
  288. if ((viodev->cmo.entitled > viodev->cmo.allocated) &&
  289. (viodev->cmo.entitled > VIO_CMO_MIN_ENT))
  290. avail += viodev->cmo.entitled -
  291. max_t(size_t, viodev->cmo.allocated,
  292. VIO_CMO_MIN_ENT);
  293. }
  294. if (delta <= avail) {
  295. vio_cmo.entitled -= delta;
  296. /* Take entitlement from the excess pool first */
  297. tmp = min(vio_cmo.excess.free, delta);
  298. vio_cmo.excess.size -= tmp;
  299. vio_cmo.excess.free -= tmp;
  300. delta -= tmp;
  301. /*
  302. * Remove all but VIO_CMO_MIN_ENT bytes from devices
  303. * until entitlement change is served
  304. */
  305. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  306. if (!delta)
  307. break;
  308. viodev = dev_ent->viodev;
  309. tmp = 0;
  310. if ((viodev->cmo.entitled > viodev->cmo.allocated) &&
  311. (viodev->cmo.entitled > VIO_CMO_MIN_ENT))
  312. tmp = viodev->cmo.entitled -
  313. max_t(size_t, viodev->cmo.allocated,
  314. VIO_CMO_MIN_ENT);
  315. viodev->cmo.entitled -= min(tmp, delta);
  316. delta -= min(tmp, delta);
  317. }
  318. } else {
  319. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  320. return -ENOMEM;
  321. }
  322. out:
  323. schedule_delayed_work(&vio_cmo.balance_q, 0);
  324. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  325. return 0;
  326. }
  327. /**
  328. * vio_cmo_balance - Balance entitlement among devices
  329. *
  330. * @work: work queue structure for this operation
  331. *
  332. * Any system entitlement above the minimum needed for devices, or
  333. * already allocated to devices, can be distributed to the devices.
  334. * The list of devices is iterated through to recalculate the desired
  335. * entitlement level and to determine how much entitlement above the
  336. * minimum entitlement is allocated to devices.
  337. *
  338. * Small chunks of the available entitlement are given to devices until
  339. * their requirements are fulfilled or there is no entitlement left to give.
  340. * Upon completion sizes of the reserve and excess pools are calculated.
  341. *
  342. * The system minimum entitlement level is also recalculated here.
  343. * Entitlement will be reserved for devices even after vio_bus_remove to
  344. * accommodate reloading the driver. The OF tree is walked to count the
  345. * number of devices present and this will remove entitlement for devices
  346. * that have actually left the system after having vio_bus_remove called.
  347. */
  348. static void vio_cmo_balance(struct work_struct *work)
  349. {
  350. struct vio_cmo *cmo;
  351. struct vio_dev *viodev;
  352. struct vio_cmo_dev_entry *dev_ent;
  353. unsigned long flags;
  354. size_t avail = 0, level, chunk, need;
  355. int devcount = 0, fulfilled;
  356. cmo = container_of(work, struct vio_cmo, balance_q.work);
  357. spin_lock_irqsave(&vio_cmo.lock, flags);
  358. /* Calculate minimum entitlement and fulfill spare */
  359. cmo->min = vio_cmo_num_OF_devs() * VIO_CMO_MIN_ENT;
  360. BUG_ON(cmo->min > cmo->entitled);
  361. cmo->spare = min_t(size_t, VIO_CMO_MIN_ENT, (cmo->entitled - cmo->min));
  362. cmo->min += cmo->spare;
  363. cmo->desired = cmo->min;
  364. /*
  365. * Determine how much entitlement is available and reset device
  366. * entitlements
  367. */
  368. avail = cmo->entitled - cmo->spare;
  369. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  370. viodev = dev_ent->viodev;
  371. devcount++;
  372. viodev->cmo.entitled = VIO_CMO_MIN_ENT;
  373. cmo->desired += (viodev->cmo.desired - VIO_CMO_MIN_ENT);
  374. avail -= max_t(size_t, viodev->cmo.allocated, VIO_CMO_MIN_ENT);
  375. }
  376. /*
  377. * Having provided each device with the minimum entitlement, loop
  378. * over the devices portioning out the remaining entitlement
  379. * until there is nothing left.
  380. */
  381. level = VIO_CMO_MIN_ENT;
  382. while (avail) {
  383. fulfilled = 0;
  384. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  385. viodev = dev_ent->viodev;
  386. if (viodev->cmo.desired <= level) {
  387. fulfilled++;
  388. continue;
  389. }
  390. /*
  391. * Give the device up to VIO_CMO_BALANCE_CHUNK
  392. * bytes of entitlement, but do not exceed the
  393. * desired level of entitlement for the device.
  394. */
  395. chunk = min_t(size_t, avail, VIO_CMO_BALANCE_CHUNK);
  396. chunk = min(chunk, (viodev->cmo.desired -
  397. viodev->cmo.entitled));
  398. viodev->cmo.entitled += chunk;
  399. /*
  400. * If the memory for this entitlement increase was
  401. * already allocated to the device it does not come
  402. * from the available pool being portioned out.
  403. */
  404. need = max(viodev->cmo.allocated, viodev->cmo.entitled)-
  405. max(viodev->cmo.allocated, level);
  406. avail -= need;
  407. }
  408. if (fulfilled == devcount)
  409. break;
  410. level += VIO_CMO_BALANCE_CHUNK;
  411. }
  412. /* Calculate new reserve and excess pool sizes */
  413. cmo->reserve.size = cmo->min;
  414. cmo->excess.free = 0;
  415. cmo->excess.size = 0;
  416. need = 0;
  417. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  418. viodev = dev_ent->viodev;
  419. /* Calculated reserve size above the minimum entitlement */
  420. if (viodev->cmo.entitled)
  421. cmo->reserve.size += (viodev->cmo.entitled -
  422. VIO_CMO_MIN_ENT);
  423. /* Calculated used excess entitlement */
  424. if (viodev->cmo.allocated > viodev->cmo.entitled)
  425. need += viodev->cmo.allocated - viodev->cmo.entitled;
  426. }
  427. cmo->excess.size = cmo->entitled - cmo->reserve.size;
  428. cmo->excess.free = cmo->excess.size - need;
  429. cancel_delayed_work(to_delayed_work(work));
  430. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  431. }
  432. static void *vio_dma_iommu_alloc_coherent(struct device *dev, size_t size,
  433. dma_addr_t *dma_handle, gfp_t flag)
  434. {
  435. struct vio_dev *viodev = to_vio_dev(dev);
  436. void *ret;
  437. if (vio_cmo_alloc(viodev, roundup(size, PAGE_SIZE))) {
  438. atomic_inc(&viodev->cmo.allocs_failed);
  439. return NULL;
  440. }
  441. ret = dma_iommu_ops.alloc_coherent(dev, size, dma_handle, flag);
  442. if (unlikely(ret == NULL)) {
  443. vio_cmo_dealloc(viodev, roundup(size, PAGE_SIZE));
  444. atomic_inc(&viodev->cmo.allocs_failed);
  445. }
  446. return ret;
  447. }
  448. static void vio_dma_iommu_free_coherent(struct device *dev, size_t size,
  449. void *vaddr, dma_addr_t dma_handle)
  450. {
  451. struct vio_dev *viodev = to_vio_dev(dev);
  452. dma_iommu_ops.free_coherent(dev, size, vaddr, dma_handle);
  453. vio_cmo_dealloc(viodev, roundup(size, PAGE_SIZE));
  454. }
  455. static dma_addr_t vio_dma_iommu_map_page(struct device *dev, struct page *page,
  456. unsigned long offset, size_t size,
  457. enum dma_data_direction direction,
  458. struct dma_attrs *attrs)
  459. {
  460. struct vio_dev *viodev = to_vio_dev(dev);
  461. dma_addr_t ret = DMA_ERROR_CODE;
  462. if (vio_cmo_alloc(viodev, roundup(size, IOMMU_PAGE_SIZE))) {
  463. atomic_inc(&viodev->cmo.allocs_failed);
  464. return ret;
  465. }
  466. ret = dma_iommu_ops.map_page(dev, page, offset, size, direction, attrs);
  467. if (unlikely(dma_mapping_error(dev, ret))) {
  468. vio_cmo_dealloc(viodev, roundup(size, IOMMU_PAGE_SIZE));
  469. atomic_inc(&viodev->cmo.allocs_failed);
  470. }
  471. return ret;
  472. }
  473. static void vio_dma_iommu_unmap_page(struct device *dev, dma_addr_t dma_handle,
  474. size_t size,
  475. enum dma_data_direction direction,
  476. struct dma_attrs *attrs)
  477. {
  478. struct vio_dev *viodev = to_vio_dev(dev);
  479. dma_iommu_ops.unmap_page(dev, dma_handle, size, direction, attrs);
  480. vio_cmo_dealloc(viodev, roundup(size, IOMMU_PAGE_SIZE));
  481. }
  482. static int vio_dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
  483. int nelems, enum dma_data_direction direction,
  484. struct dma_attrs *attrs)
  485. {
  486. struct vio_dev *viodev = to_vio_dev(dev);
  487. struct scatterlist *sgl;
  488. int ret, count = 0;
  489. size_t alloc_size = 0;
  490. for (sgl = sglist; count < nelems; count++, sgl++)
  491. alloc_size += roundup(sgl->length, IOMMU_PAGE_SIZE);
  492. if (vio_cmo_alloc(viodev, alloc_size)) {
  493. atomic_inc(&viodev->cmo.allocs_failed);
  494. return 0;
  495. }
  496. ret = dma_iommu_ops.map_sg(dev, sglist, nelems, direction, attrs);
  497. if (unlikely(!ret)) {
  498. vio_cmo_dealloc(viodev, alloc_size);
  499. atomic_inc(&viodev->cmo.allocs_failed);
  500. return ret;
  501. }
  502. for (sgl = sglist, count = 0; count < ret; count++, sgl++)
  503. alloc_size -= roundup(sgl->dma_length, IOMMU_PAGE_SIZE);
  504. if (alloc_size)
  505. vio_cmo_dealloc(viodev, alloc_size);
  506. return ret;
  507. }
  508. static void vio_dma_iommu_unmap_sg(struct device *dev,
  509. struct scatterlist *sglist, int nelems,
  510. enum dma_data_direction direction,
  511. struct dma_attrs *attrs)
  512. {
  513. struct vio_dev *viodev = to_vio_dev(dev);
  514. struct scatterlist *sgl;
  515. size_t alloc_size = 0;
  516. int count = 0;
  517. for (sgl = sglist; count < nelems; count++, sgl++)
  518. alloc_size += roundup(sgl->dma_length, IOMMU_PAGE_SIZE);
  519. dma_iommu_ops.unmap_sg(dev, sglist, nelems, direction, attrs);
  520. vio_cmo_dealloc(viodev, alloc_size);
  521. }
  522. struct dma_mapping_ops vio_dma_mapping_ops = {
  523. .alloc_coherent = vio_dma_iommu_alloc_coherent,
  524. .free_coherent = vio_dma_iommu_free_coherent,
  525. .map_sg = vio_dma_iommu_map_sg,
  526. .unmap_sg = vio_dma_iommu_unmap_sg,
  527. .map_page = vio_dma_iommu_map_page,
  528. .unmap_page = vio_dma_iommu_unmap_page,
  529. };
  530. /**
  531. * vio_cmo_set_dev_desired - Set desired entitlement for a device
  532. *
  533. * @viodev: struct vio_dev for device to alter
  534. * @new_desired: new desired entitlement level in bytes
  535. *
  536. * For use by devices to request a change to their entitlement at runtime or
  537. * through sysfs. The desired entitlement level is changed and a balancing
  538. * of system resources is scheduled to run in the future.
  539. */
  540. void vio_cmo_set_dev_desired(struct vio_dev *viodev, size_t desired)
  541. {
  542. unsigned long flags;
  543. struct vio_cmo_dev_entry *dev_ent;
  544. int found = 0;
  545. if (!firmware_has_feature(FW_FEATURE_CMO))
  546. return;
  547. spin_lock_irqsave(&vio_cmo.lock, flags);
  548. if (desired < VIO_CMO_MIN_ENT)
  549. desired = VIO_CMO_MIN_ENT;
  550. /*
  551. * Changes will not be made for devices not in the device list.
  552. * If it is not in the device list, then no driver is loaded
  553. * for the device and it can not receive entitlement.
  554. */
  555. list_for_each_entry(dev_ent, &vio_cmo.device_list, list)
  556. if (viodev == dev_ent->viodev) {
  557. found = 1;
  558. break;
  559. }
  560. if (!found)
  561. return;
  562. /* Increase/decrease in desired device entitlement */
  563. if (desired >= viodev->cmo.desired) {
  564. /* Just bump the bus and device values prior to a balance*/
  565. vio_cmo.desired += desired - viodev->cmo.desired;
  566. viodev->cmo.desired = desired;
  567. } else {
  568. /* Decrease bus and device values for desired entitlement */
  569. vio_cmo.desired -= viodev->cmo.desired - desired;
  570. viodev->cmo.desired = desired;
  571. /*
  572. * If less entitlement is desired than current entitlement, move
  573. * any reserve memory in the change region to the excess pool.
  574. */
  575. if (viodev->cmo.entitled > desired) {
  576. vio_cmo.reserve.size -= viodev->cmo.entitled - desired;
  577. vio_cmo.excess.size += viodev->cmo.entitled - desired;
  578. /*
  579. * If entitlement moving from the reserve pool to the
  580. * excess pool is currently unused, add to the excess
  581. * free counter.
  582. */
  583. if (viodev->cmo.allocated < viodev->cmo.entitled)
  584. vio_cmo.excess.free += viodev->cmo.entitled -
  585. max(viodev->cmo.allocated, desired);
  586. viodev->cmo.entitled = desired;
  587. }
  588. }
  589. schedule_delayed_work(&vio_cmo.balance_q, 0);
  590. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  591. }
  592. /**
  593. * vio_cmo_bus_probe - Handle CMO specific bus probe activities
  594. *
  595. * @viodev - Pointer to struct vio_dev for device
  596. *
  597. * Determine the devices IO memory entitlement needs, attempting
  598. * to satisfy the system minimum entitlement at first and scheduling
  599. * a balance operation to take care of the rest at a later time.
  600. *
  601. * Returns: 0 on success, -EINVAL when device doesn't support CMO, and
  602. * -ENOMEM when entitlement is not available for device or
  603. * device entry.
  604. *
  605. */
  606. static int vio_cmo_bus_probe(struct vio_dev *viodev)
  607. {
  608. struct vio_cmo_dev_entry *dev_ent;
  609. struct device *dev = &viodev->dev;
  610. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  611. unsigned long flags;
  612. size_t size;
  613. /*
  614. * Check to see that device has a DMA window and configure
  615. * entitlement for the device.
  616. */
  617. if (of_get_property(viodev->dev.archdata.of_node,
  618. "ibm,my-dma-window", NULL)) {
  619. /* Check that the driver is CMO enabled and get desired DMA */
  620. if (!viodrv->get_desired_dma) {
  621. dev_err(dev, "%s: device driver does not support CMO\n",
  622. __func__);
  623. return -EINVAL;
  624. }
  625. viodev->cmo.desired = IOMMU_PAGE_ALIGN(viodrv->get_desired_dma(viodev));
  626. if (viodev->cmo.desired < VIO_CMO_MIN_ENT)
  627. viodev->cmo.desired = VIO_CMO_MIN_ENT;
  628. size = VIO_CMO_MIN_ENT;
  629. dev_ent = kmalloc(sizeof(struct vio_cmo_dev_entry),
  630. GFP_KERNEL);
  631. if (!dev_ent)
  632. return -ENOMEM;
  633. dev_ent->viodev = viodev;
  634. spin_lock_irqsave(&vio_cmo.lock, flags);
  635. list_add(&dev_ent->list, &vio_cmo.device_list);
  636. } else {
  637. viodev->cmo.desired = 0;
  638. size = 0;
  639. spin_lock_irqsave(&vio_cmo.lock, flags);
  640. }
  641. /*
  642. * If the needs for vio_cmo.min have not changed since they
  643. * were last set, the number of devices in the OF tree has
  644. * been constant and the IO memory for this is already in
  645. * the reserve pool.
  646. */
  647. if (vio_cmo.min == ((vio_cmo_num_OF_devs() + 1) *
  648. VIO_CMO_MIN_ENT)) {
  649. /* Updated desired entitlement if device requires it */
  650. if (size)
  651. vio_cmo.desired += (viodev->cmo.desired -
  652. VIO_CMO_MIN_ENT);
  653. } else {
  654. size_t tmp;
  655. tmp = vio_cmo.spare + vio_cmo.excess.free;
  656. if (tmp < size) {
  657. dev_err(dev, "%s: insufficient free "
  658. "entitlement to add device. "
  659. "Need %lu, have %lu\n", __func__,
  660. size, (vio_cmo.spare + tmp));
  661. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  662. return -ENOMEM;
  663. }
  664. /* Use excess pool first to fulfill request */
  665. tmp = min(size, vio_cmo.excess.free);
  666. vio_cmo.excess.free -= tmp;
  667. vio_cmo.excess.size -= tmp;
  668. vio_cmo.reserve.size += tmp;
  669. /* Use spare if excess pool was insufficient */
  670. vio_cmo.spare -= size - tmp;
  671. /* Update bus accounting */
  672. vio_cmo.min += size;
  673. vio_cmo.desired += viodev->cmo.desired;
  674. }
  675. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  676. return 0;
  677. }
  678. /**
  679. * vio_cmo_bus_remove - Handle CMO specific bus removal activities
  680. *
  681. * @viodev - Pointer to struct vio_dev for device
  682. *
  683. * Remove the device from the cmo device list. The minimum entitlement
  684. * will be reserved for the device as long as it is in the system. The
  685. * rest of the entitlement the device had been allocated will be returned
  686. * to the system.
  687. */
  688. static void vio_cmo_bus_remove(struct vio_dev *viodev)
  689. {
  690. struct vio_cmo_dev_entry *dev_ent;
  691. unsigned long flags;
  692. size_t tmp;
  693. spin_lock_irqsave(&vio_cmo.lock, flags);
  694. if (viodev->cmo.allocated) {
  695. dev_err(&viodev->dev, "%s: device had %lu bytes of IO "
  696. "allocated after remove operation.\n",
  697. __func__, viodev->cmo.allocated);
  698. BUG();
  699. }
  700. /*
  701. * Remove the device from the device list being maintained for
  702. * CMO enabled devices.
  703. */
  704. list_for_each_entry(dev_ent, &vio_cmo.device_list, list)
  705. if (viodev == dev_ent->viodev) {
  706. list_del(&dev_ent->list);
  707. kfree(dev_ent);
  708. break;
  709. }
  710. /*
  711. * Devices may not require any entitlement and they do not need
  712. * to be processed. Otherwise, return the device's entitlement
  713. * back to the pools.
  714. */
  715. if (viodev->cmo.entitled) {
  716. /*
  717. * This device has not yet left the OF tree, it's
  718. * minimum entitlement remains in vio_cmo.min and
  719. * vio_cmo.desired
  720. */
  721. vio_cmo.desired -= (viodev->cmo.desired - VIO_CMO_MIN_ENT);
  722. /*
  723. * Save min allocation for device in reserve as long
  724. * as it exists in OF tree as determined by later
  725. * balance operation
  726. */
  727. viodev->cmo.entitled -= VIO_CMO_MIN_ENT;
  728. /* Replenish spare from freed reserve pool */
  729. if (viodev->cmo.entitled && (vio_cmo.spare < VIO_CMO_MIN_ENT)) {
  730. tmp = min(viodev->cmo.entitled, (VIO_CMO_MIN_ENT -
  731. vio_cmo.spare));
  732. vio_cmo.spare += tmp;
  733. viodev->cmo.entitled -= tmp;
  734. }
  735. /* Remaining reserve goes to excess pool */
  736. vio_cmo.excess.size += viodev->cmo.entitled;
  737. vio_cmo.excess.free += viodev->cmo.entitled;
  738. vio_cmo.reserve.size -= viodev->cmo.entitled;
  739. /*
  740. * Until the device is removed it will keep a
  741. * minimum entitlement; this will guarantee that
  742. * a module unload/load will result in a success.
  743. */
  744. viodev->cmo.entitled = VIO_CMO_MIN_ENT;
  745. viodev->cmo.desired = VIO_CMO_MIN_ENT;
  746. atomic_set(&viodev->cmo.allocs_failed, 0);
  747. }
  748. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  749. }
  750. static void vio_cmo_set_dma_ops(struct vio_dev *viodev)
  751. {
  752. vio_dma_mapping_ops.dma_supported = dma_iommu_ops.dma_supported;
  753. viodev->dev.archdata.dma_ops = &vio_dma_mapping_ops;
  754. }
  755. /**
  756. * vio_cmo_bus_init - CMO entitlement initialization at bus init time
  757. *
  758. * Set up the reserve and excess entitlement pools based on available
  759. * system entitlement and the number of devices in the OF tree that
  760. * require entitlement in the reserve pool.
  761. */
  762. static void vio_cmo_bus_init(void)
  763. {
  764. struct hvcall_mpp_data mpp_data;
  765. int err;
  766. memset(&vio_cmo, 0, sizeof(struct vio_cmo));
  767. spin_lock_init(&vio_cmo.lock);
  768. INIT_LIST_HEAD(&vio_cmo.device_list);
  769. INIT_DELAYED_WORK(&vio_cmo.balance_q, vio_cmo_balance);
  770. /* Get current system entitlement */
  771. err = h_get_mpp(&mpp_data);
  772. /*
  773. * On failure, continue with entitlement set to 0, will panic()
  774. * later when spare is reserved.
  775. */
  776. if (err != H_SUCCESS) {
  777. printk(KERN_ERR "%s: unable to determine system IO "\
  778. "entitlement. (%d)\n", __func__, err);
  779. vio_cmo.entitled = 0;
  780. } else {
  781. vio_cmo.entitled = mpp_data.entitled_mem;
  782. }
  783. /* Set reservation and check against entitlement */
  784. vio_cmo.spare = VIO_CMO_MIN_ENT;
  785. vio_cmo.reserve.size = vio_cmo.spare;
  786. vio_cmo.reserve.size += (vio_cmo_num_OF_devs() *
  787. VIO_CMO_MIN_ENT);
  788. if (vio_cmo.reserve.size > vio_cmo.entitled) {
  789. printk(KERN_ERR "%s: insufficient system entitlement\n",
  790. __func__);
  791. panic("%s: Insufficient system entitlement", __func__);
  792. }
  793. /* Set the remaining accounting variables */
  794. vio_cmo.excess.size = vio_cmo.entitled - vio_cmo.reserve.size;
  795. vio_cmo.excess.free = vio_cmo.excess.size;
  796. vio_cmo.min = vio_cmo.reserve.size;
  797. vio_cmo.desired = vio_cmo.reserve.size;
  798. }
  799. /* sysfs device functions and data structures for CMO */
  800. #define viodev_cmo_rd_attr(name) \
  801. static ssize_t viodev_cmo_##name##_show(struct device *dev, \
  802. struct device_attribute *attr, \
  803. char *buf) \
  804. { \
  805. return sprintf(buf, "%lu\n", to_vio_dev(dev)->cmo.name); \
  806. }
  807. static ssize_t viodev_cmo_allocs_failed_show(struct device *dev,
  808. struct device_attribute *attr, char *buf)
  809. {
  810. struct vio_dev *viodev = to_vio_dev(dev);
  811. return sprintf(buf, "%d\n", atomic_read(&viodev->cmo.allocs_failed));
  812. }
  813. static ssize_t viodev_cmo_allocs_failed_reset(struct device *dev,
  814. struct device_attribute *attr, const char *buf, size_t count)
  815. {
  816. struct vio_dev *viodev = to_vio_dev(dev);
  817. atomic_set(&viodev->cmo.allocs_failed, 0);
  818. return count;
  819. }
  820. static ssize_t viodev_cmo_desired_set(struct device *dev,
  821. struct device_attribute *attr, const char *buf, size_t count)
  822. {
  823. struct vio_dev *viodev = to_vio_dev(dev);
  824. size_t new_desired;
  825. int ret;
  826. ret = strict_strtoul(buf, 10, &new_desired);
  827. if (ret)
  828. return ret;
  829. vio_cmo_set_dev_desired(viodev, new_desired);
  830. return count;
  831. }
  832. viodev_cmo_rd_attr(desired);
  833. viodev_cmo_rd_attr(entitled);
  834. viodev_cmo_rd_attr(allocated);
  835. static ssize_t name_show(struct device *, struct device_attribute *, char *);
  836. static ssize_t devspec_show(struct device *, struct device_attribute *, char *);
  837. static struct device_attribute vio_cmo_dev_attrs[] = {
  838. __ATTR_RO(name),
  839. __ATTR_RO(devspec),
  840. __ATTR(cmo_desired, S_IWUSR|S_IRUSR|S_IWGRP|S_IRGRP|S_IROTH,
  841. viodev_cmo_desired_show, viodev_cmo_desired_set),
  842. __ATTR(cmo_entitled, S_IRUGO, viodev_cmo_entitled_show, NULL),
  843. __ATTR(cmo_allocated, S_IRUGO, viodev_cmo_allocated_show, NULL),
  844. __ATTR(cmo_allocs_failed, S_IWUSR|S_IRUSR|S_IWGRP|S_IRGRP|S_IROTH,
  845. viodev_cmo_allocs_failed_show, viodev_cmo_allocs_failed_reset),
  846. __ATTR_NULL
  847. };
  848. /* sysfs bus functions and data structures for CMO */
  849. #define viobus_cmo_rd_attr(name) \
  850. static ssize_t \
  851. viobus_cmo_##name##_show(struct bus_type *bt, char *buf) \
  852. { \
  853. return sprintf(buf, "%lu\n", vio_cmo.name); \
  854. }
  855. #define viobus_cmo_pool_rd_attr(name, var) \
  856. static ssize_t \
  857. viobus_cmo_##name##_pool_show_##var(struct bus_type *bt, char *buf) \
  858. { \
  859. return sprintf(buf, "%lu\n", vio_cmo.name.var); \
  860. }
  861. static ssize_t viobus_cmo_high_reset(struct bus_type *bt, const char *buf,
  862. size_t count)
  863. {
  864. unsigned long flags;
  865. spin_lock_irqsave(&vio_cmo.lock, flags);
  866. vio_cmo.high = vio_cmo.curr;
  867. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  868. return count;
  869. }
  870. viobus_cmo_rd_attr(entitled);
  871. viobus_cmo_pool_rd_attr(reserve, size);
  872. viobus_cmo_pool_rd_attr(excess, size);
  873. viobus_cmo_pool_rd_attr(excess, free);
  874. viobus_cmo_rd_attr(spare);
  875. viobus_cmo_rd_attr(min);
  876. viobus_cmo_rd_attr(desired);
  877. viobus_cmo_rd_attr(curr);
  878. viobus_cmo_rd_attr(high);
  879. static struct bus_attribute vio_cmo_bus_attrs[] = {
  880. __ATTR(cmo_entitled, S_IRUGO, viobus_cmo_entitled_show, NULL),
  881. __ATTR(cmo_reserve_size, S_IRUGO, viobus_cmo_reserve_pool_show_size, NULL),
  882. __ATTR(cmo_excess_size, S_IRUGO, viobus_cmo_excess_pool_show_size, NULL),
  883. __ATTR(cmo_excess_free, S_IRUGO, viobus_cmo_excess_pool_show_free, NULL),
  884. __ATTR(cmo_spare, S_IRUGO, viobus_cmo_spare_show, NULL),
  885. __ATTR(cmo_min, S_IRUGO, viobus_cmo_min_show, NULL),
  886. __ATTR(cmo_desired, S_IRUGO, viobus_cmo_desired_show, NULL),
  887. __ATTR(cmo_curr, S_IRUGO, viobus_cmo_curr_show, NULL),
  888. __ATTR(cmo_high, S_IWUSR|S_IRUSR|S_IWGRP|S_IRGRP|S_IROTH,
  889. viobus_cmo_high_show, viobus_cmo_high_reset),
  890. __ATTR_NULL
  891. };
  892. static void vio_cmo_sysfs_init(void)
  893. {
  894. vio_bus_type.dev_attrs = vio_cmo_dev_attrs;
  895. vio_bus_type.bus_attrs = vio_cmo_bus_attrs;
  896. }
  897. #else /* CONFIG_PPC_SMLPAR */
  898. /* Dummy functions for iSeries platform */
  899. int vio_cmo_entitlement_update(size_t new_entitlement) { return 0; }
  900. void vio_cmo_set_dev_desired(struct vio_dev *viodev, size_t desired) {}
  901. static int vio_cmo_bus_probe(struct vio_dev *viodev) { return 0; }
  902. static void vio_cmo_bus_remove(struct vio_dev *viodev) {}
  903. static void vio_cmo_set_dma_ops(struct vio_dev *viodev) {}
  904. static void vio_cmo_bus_init(void) {}
  905. static void vio_cmo_sysfs_init(void) { }
  906. #endif /* CONFIG_PPC_SMLPAR */
  907. EXPORT_SYMBOL(vio_cmo_entitlement_update);
  908. EXPORT_SYMBOL(vio_cmo_set_dev_desired);
  909. static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
  910. {
  911. const unsigned char *dma_window;
  912. struct iommu_table *tbl;
  913. unsigned long offset, size;
  914. if (firmware_has_feature(FW_FEATURE_ISERIES))
  915. return vio_build_iommu_table_iseries(dev);
  916. dma_window = of_get_property(dev->dev.archdata.of_node,
  917. "ibm,my-dma-window", NULL);
  918. if (!dma_window)
  919. return NULL;
  920. tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
  921. of_parse_dma_window(dev->dev.archdata.of_node, dma_window,
  922. &tbl->it_index, &offset, &size);
  923. /* TCE table size - measured in tce entries */
  924. tbl->it_size = size >> IOMMU_PAGE_SHIFT;
  925. /* offset for VIO should always be 0 */
  926. tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
  927. tbl->it_busno = 0;
  928. tbl->it_type = TCE_VB;
  929. return iommu_init_table(tbl, -1);
  930. }
  931. /**
  932. * vio_match_device: - Tell if a VIO device has a matching
  933. * VIO device id structure.
  934. * @ids: array of VIO device id structures to search in
  935. * @dev: the VIO device structure to match against
  936. *
  937. * Used by a driver to check whether a VIO device present in the
  938. * system is in its list of supported devices. Returns the matching
  939. * vio_device_id structure or NULL if there is no match.
  940. */
  941. static const struct vio_device_id *vio_match_device(
  942. const struct vio_device_id *ids, const struct vio_dev *dev)
  943. {
  944. while (ids->type[0] != '\0') {
  945. if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
  946. of_device_is_compatible(dev->dev.archdata.of_node,
  947. ids->compat))
  948. return ids;
  949. ids++;
  950. }
  951. return NULL;
  952. }
  953. /*
  954. * Convert from struct device to struct vio_dev and pass to driver.
  955. * dev->driver has already been set by generic code because vio_bus_match
  956. * succeeded.
  957. */
  958. static int vio_bus_probe(struct device *dev)
  959. {
  960. struct vio_dev *viodev = to_vio_dev(dev);
  961. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  962. const struct vio_device_id *id;
  963. int error = -ENODEV;
  964. if (!viodrv->probe)
  965. return error;
  966. id = vio_match_device(viodrv->id_table, viodev);
  967. if (id) {
  968. memset(&viodev->cmo, 0, sizeof(viodev->cmo));
  969. if (firmware_has_feature(FW_FEATURE_CMO)) {
  970. error = vio_cmo_bus_probe(viodev);
  971. if (error)
  972. return error;
  973. }
  974. error = viodrv->probe(viodev, id);
  975. if (error && firmware_has_feature(FW_FEATURE_CMO))
  976. vio_cmo_bus_remove(viodev);
  977. }
  978. return error;
  979. }
  980. /* convert from struct device to struct vio_dev and pass to driver. */
  981. static int vio_bus_remove(struct device *dev)
  982. {
  983. struct vio_dev *viodev = to_vio_dev(dev);
  984. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  985. struct device *devptr;
  986. int ret = 1;
  987. /*
  988. * Hold a reference to the device after the remove function is called
  989. * to allow for CMO accounting cleanup for the device.
  990. */
  991. devptr = get_device(dev);
  992. if (viodrv->remove)
  993. ret = viodrv->remove(viodev);
  994. if (!ret && firmware_has_feature(FW_FEATURE_CMO))
  995. vio_cmo_bus_remove(viodev);
  996. put_device(devptr);
  997. return ret;
  998. }
  999. /**
  1000. * vio_register_driver: - Register a new vio driver
  1001. * @drv: The vio_driver structure to be registered.
  1002. */
  1003. int vio_register_driver(struct vio_driver *viodrv)
  1004. {
  1005. printk(KERN_DEBUG "%s: driver %s registering\n", __func__,
  1006. viodrv->driver.name);
  1007. /* fill in 'struct driver' fields */
  1008. viodrv->driver.bus = &vio_bus_type;
  1009. return driver_register(&viodrv->driver);
  1010. }
  1011. EXPORT_SYMBOL(vio_register_driver);
  1012. /**
  1013. * vio_unregister_driver - Remove registration of vio driver.
  1014. * @driver: The vio_driver struct to be removed form registration
  1015. */
  1016. void vio_unregister_driver(struct vio_driver *viodrv)
  1017. {
  1018. driver_unregister(&viodrv->driver);
  1019. }
  1020. EXPORT_SYMBOL(vio_unregister_driver);
  1021. /* vio_dev refcount hit 0 */
  1022. static void __devinit vio_dev_release(struct device *dev)
  1023. {
  1024. /* XXX should free TCE table */
  1025. of_node_put(dev->archdata.of_node);
  1026. kfree(to_vio_dev(dev));
  1027. }
  1028. /**
  1029. * vio_register_device_node: - Register a new vio device.
  1030. * @of_node: The OF node for this device.
  1031. *
  1032. * Creates and initializes a vio_dev structure from the data in
  1033. * of_node and adds it to the list of virtual devices.
  1034. * Returns a pointer to the created vio_dev or NULL if node has
  1035. * NULL device_type or compatible fields.
  1036. */
  1037. struct vio_dev *vio_register_device_node(struct device_node *of_node)
  1038. {
  1039. struct vio_dev *viodev;
  1040. const unsigned int *unit_address;
  1041. /* we need the 'device_type' property, in order to match with drivers */
  1042. if (of_node->type == NULL) {
  1043. printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
  1044. __func__,
  1045. of_node->name ? of_node->name : "<unknown>");
  1046. return NULL;
  1047. }
  1048. unit_address = of_get_property(of_node, "reg", NULL);
  1049. if (unit_address == NULL) {
  1050. printk(KERN_WARNING "%s: node %s missing 'reg'\n",
  1051. __func__,
  1052. of_node->name ? of_node->name : "<unknown>");
  1053. return NULL;
  1054. }
  1055. /* allocate a vio_dev for this node */
  1056. viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
  1057. if (viodev == NULL)
  1058. return NULL;
  1059. viodev->irq = irq_of_parse_and_map(of_node, 0);
  1060. dev_set_name(&viodev->dev, "%x", *unit_address);
  1061. viodev->name = of_node->name;
  1062. viodev->type = of_node->type;
  1063. viodev->unit_address = *unit_address;
  1064. if (firmware_has_feature(FW_FEATURE_ISERIES)) {
  1065. unit_address = of_get_property(of_node,
  1066. "linux,unit_address", NULL);
  1067. if (unit_address != NULL)
  1068. viodev->unit_address = *unit_address;
  1069. }
  1070. viodev->dev.archdata.of_node = of_node_get(of_node);
  1071. if (firmware_has_feature(FW_FEATURE_CMO))
  1072. vio_cmo_set_dma_ops(viodev);
  1073. else
  1074. viodev->dev.archdata.dma_ops = &dma_iommu_ops;
  1075. viodev->dev.archdata.dma_data = vio_build_iommu_table(viodev);
  1076. set_dev_node(&viodev->dev, of_node_to_nid(of_node));
  1077. /* init generic 'struct device' fields: */
  1078. viodev->dev.parent = &vio_bus_device.dev;
  1079. viodev->dev.bus = &vio_bus_type;
  1080. viodev->dev.release = vio_dev_release;
  1081. /* register with generic device framework */
  1082. if (device_register(&viodev->dev)) {
  1083. printk(KERN_ERR "%s: failed to register device %s\n",
  1084. __func__, dev_name(&viodev->dev));
  1085. /* XXX free TCE table */
  1086. kfree(viodev);
  1087. return NULL;
  1088. }
  1089. return viodev;
  1090. }
  1091. EXPORT_SYMBOL(vio_register_device_node);
  1092. /**
  1093. * vio_bus_init: - Initialize the virtual IO bus
  1094. */
  1095. static int __init vio_bus_init(void)
  1096. {
  1097. int err;
  1098. struct device_node *node_vroot;
  1099. if (firmware_has_feature(FW_FEATURE_CMO))
  1100. vio_cmo_sysfs_init();
  1101. err = bus_register(&vio_bus_type);
  1102. if (err) {
  1103. printk(KERN_ERR "failed to register VIO bus\n");
  1104. return err;
  1105. }
  1106. /*
  1107. * The fake parent of all vio devices, just to give us
  1108. * a nice directory
  1109. */
  1110. err = device_register(&vio_bus_device.dev);
  1111. if (err) {
  1112. printk(KERN_WARNING "%s: device_register returned %i\n",
  1113. __func__, err);
  1114. return err;
  1115. }
  1116. if (firmware_has_feature(FW_FEATURE_CMO))
  1117. vio_cmo_bus_init();
  1118. node_vroot = of_find_node_by_name(NULL, "vdevice");
  1119. if (node_vroot) {
  1120. struct device_node *of_node;
  1121. /*
  1122. * Create struct vio_devices for each virtual device in
  1123. * the device tree. Drivers will associate with them later.
  1124. */
  1125. for (of_node = node_vroot->child; of_node != NULL;
  1126. of_node = of_node->sibling)
  1127. vio_register_device_node(of_node);
  1128. of_node_put(node_vroot);
  1129. }
  1130. return 0;
  1131. }
  1132. __initcall(vio_bus_init);
  1133. static ssize_t name_show(struct device *dev,
  1134. struct device_attribute *attr, char *buf)
  1135. {
  1136. return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
  1137. }
  1138. static ssize_t devspec_show(struct device *dev,
  1139. struct device_attribute *attr, char *buf)
  1140. {
  1141. struct device_node *of_node = dev->archdata.of_node;
  1142. return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
  1143. }
  1144. static struct device_attribute vio_dev_attrs[] = {
  1145. __ATTR_RO(name),
  1146. __ATTR_RO(devspec),
  1147. __ATTR_NULL
  1148. };
  1149. void __devinit vio_unregister_device(struct vio_dev *viodev)
  1150. {
  1151. device_unregister(&viodev->dev);
  1152. }
  1153. EXPORT_SYMBOL(vio_unregister_device);
  1154. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  1155. {
  1156. const struct vio_dev *vio_dev = to_vio_dev(dev);
  1157. struct vio_driver *vio_drv = to_vio_driver(drv);
  1158. const struct vio_device_id *ids = vio_drv->id_table;
  1159. return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
  1160. }
  1161. static int vio_hotplug(struct device *dev, struct kobj_uevent_env *env)
  1162. {
  1163. const struct vio_dev *vio_dev = to_vio_dev(dev);
  1164. struct device_node *dn;
  1165. const char *cp;
  1166. dn = dev->archdata.of_node;
  1167. if (!dn)
  1168. return -ENODEV;
  1169. cp = of_get_property(dn, "compatible", NULL);
  1170. if (!cp)
  1171. return -ENODEV;
  1172. add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, cp);
  1173. return 0;
  1174. }
  1175. static struct bus_type vio_bus_type = {
  1176. .name = "vio",
  1177. .dev_attrs = vio_dev_attrs,
  1178. .uevent = vio_hotplug,
  1179. .match = vio_bus_match,
  1180. .probe = vio_bus_probe,
  1181. .remove = vio_bus_remove,
  1182. };
  1183. /**
  1184. * vio_get_attribute: - get attribute for virtual device
  1185. * @vdev: The vio device to get property.
  1186. * @which: The property/attribute to be extracted.
  1187. * @length: Pointer to length of returned data size (unused if NULL).
  1188. *
  1189. * Calls prom.c's of_get_property() to return the value of the
  1190. * attribute specified by @which
  1191. */
  1192. const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
  1193. {
  1194. return of_get_property(vdev->dev.archdata.of_node, which, length);
  1195. }
  1196. EXPORT_SYMBOL(vio_get_attribute);
  1197. #ifdef CONFIG_PPC_PSERIES
  1198. /* vio_find_name() - internal because only vio.c knows how we formatted the
  1199. * kobject name
  1200. */
  1201. static struct vio_dev *vio_find_name(const char *name)
  1202. {
  1203. struct device *found;
  1204. found = bus_find_device_by_name(&vio_bus_type, NULL, name);
  1205. if (!found)
  1206. return NULL;
  1207. return to_vio_dev(found);
  1208. }
  1209. /**
  1210. * vio_find_node - find an already-registered vio_dev
  1211. * @vnode: device_node of the virtual device we're looking for
  1212. */
  1213. struct vio_dev *vio_find_node(struct device_node *vnode)
  1214. {
  1215. const uint32_t *unit_address;
  1216. char kobj_name[20];
  1217. /* construct the kobject name from the device node */
  1218. unit_address = of_get_property(vnode, "reg", NULL);
  1219. if (!unit_address)
  1220. return NULL;
  1221. snprintf(kobj_name, sizeof(kobj_name), "%x", *unit_address);
  1222. return vio_find_name(kobj_name);
  1223. }
  1224. EXPORT_SYMBOL(vio_find_node);
  1225. int vio_enable_interrupts(struct vio_dev *dev)
  1226. {
  1227. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
  1228. if (rc != H_SUCCESS)
  1229. printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
  1230. return rc;
  1231. }
  1232. EXPORT_SYMBOL(vio_enable_interrupts);
  1233. int vio_disable_interrupts(struct vio_dev *dev)
  1234. {
  1235. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
  1236. if (rc != H_SUCCESS)
  1237. printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
  1238. return rc;
  1239. }
  1240. EXPORT_SYMBOL(vio_disable_interrupts);
  1241. #endif /* CONFIG_PPC_PSERIES */