vio.c 43 KB

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