vio.c 43 KB

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