mm.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. /*
  2. * PS3 address space management.
  3. *
  4. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  5. * Copyright 2006 Sony Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/memory_hotplug.h>
  23. #include <linux/lmb.h>
  24. #include <asm/firmware.h>
  25. #include <asm/prom.h>
  26. #include <asm/udbg.h>
  27. #include <asm/lv1call.h>
  28. #include "platform.h"
  29. #if defined(DEBUG)
  30. #define DBG udbg_printf
  31. #else
  32. #define DBG pr_debug
  33. #endif
  34. enum {
  35. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  36. USE_DYNAMIC_DMA = 1,
  37. #else
  38. USE_DYNAMIC_DMA = 0,
  39. #endif
  40. };
  41. enum {
  42. PAGE_SHIFT_4K = 12U,
  43. PAGE_SHIFT_64K = 16U,
  44. PAGE_SHIFT_16M = 24U,
  45. };
  46. static unsigned long make_page_sizes(unsigned long a, unsigned long b)
  47. {
  48. return (a << 56) | (b << 48);
  49. }
  50. enum {
  51. ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
  52. ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
  53. };
  54. /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
  55. enum {
  56. HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
  57. HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
  58. };
  59. /*============================================================================*/
  60. /* virtual address space routines */
  61. /*============================================================================*/
  62. /**
  63. * struct mem_region - memory region structure
  64. * @base: base address
  65. * @size: size in bytes
  66. * @offset: difference between base and rm.size
  67. */
  68. struct mem_region {
  69. unsigned long base;
  70. unsigned long size;
  71. unsigned long offset;
  72. };
  73. /**
  74. * struct map - address space state variables holder
  75. * @total: total memory available as reported by HV
  76. * @vas_id - HV virtual address space id
  77. * @htab_size: htab size in bytes
  78. *
  79. * The HV virtual address space (vas) allows for hotplug memory regions.
  80. * Memory regions can be created and destroyed in the vas at runtime.
  81. * @rm: real mode (bootmem) region
  82. * @r1: hotplug memory region(s)
  83. *
  84. * ps3 addresses
  85. * virt_addr: a cpu 'translated' effective address
  86. * phys_addr: an address in what Linux thinks is the physical address space
  87. * lpar_addr: an address in the HV virtual address space
  88. * bus_addr: an io controller 'translated' address on a device bus
  89. */
  90. struct map {
  91. unsigned long total;
  92. unsigned long vas_id;
  93. unsigned long htab_size;
  94. struct mem_region rm;
  95. struct mem_region r1;
  96. };
  97. #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
  98. static void __maybe_unused _debug_dump_map(const struct map *m,
  99. const char *func, int line)
  100. {
  101. DBG("%s:%d: map.total = %lxh\n", func, line, m->total);
  102. DBG("%s:%d: map.rm.size = %lxh\n", func, line, m->rm.size);
  103. DBG("%s:%d: map.vas_id = %lu\n", func, line, m->vas_id);
  104. DBG("%s:%d: map.htab_size = %lxh\n", func, line, m->htab_size);
  105. DBG("%s:%d: map.r1.base = %lxh\n", func, line, m->r1.base);
  106. DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
  107. DBG("%s:%d: map.r1.size = %lxh\n", func, line, m->r1.size);
  108. }
  109. static struct map map;
  110. /**
  111. * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
  112. * @phys_addr: linux physical address
  113. */
  114. unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
  115. {
  116. BUG_ON(is_kernel_addr(phys_addr));
  117. return (phys_addr < map.rm.size || phys_addr >= map.total)
  118. ? phys_addr : phys_addr + map.r1.offset;
  119. }
  120. EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
  121. /**
  122. * ps3_mm_vas_create - create the virtual address space
  123. */
  124. void __init ps3_mm_vas_create(unsigned long* htab_size)
  125. {
  126. int result;
  127. unsigned long start_address;
  128. unsigned long size;
  129. unsigned long access_right;
  130. unsigned long max_page_size;
  131. unsigned long flags;
  132. result = lv1_query_logical_partition_address_region_info(0,
  133. &start_address, &size, &access_right, &max_page_size,
  134. &flags);
  135. if (result) {
  136. DBG("%s:%d: lv1_query_logical_partition_address_region_info "
  137. "failed: %s\n", __func__, __LINE__,
  138. ps3_result(result));
  139. goto fail;
  140. }
  141. if (max_page_size < PAGE_SHIFT_16M) {
  142. DBG("%s:%d: bad max_page_size %lxh\n", __func__, __LINE__,
  143. max_page_size);
  144. goto fail;
  145. }
  146. BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
  147. BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
  148. result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
  149. 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
  150. &map.vas_id, &map.htab_size);
  151. if (result) {
  152. DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
  153. __func__, __LINE__, ps3_result(result));
  154. goto fail;
  155. }
  156. result = lv1_select_virtual_address_space(map.vas_id);
  157. if (result) {
  158. DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
  159. __func__, __LINE__, ps3_result(result));
  160. goto fail;
  161. }
  162. *htab_size = map.htab_size;
  163. debug_dump_map(&map);
  164. return;
  165. fail:
  166. panic("ps3_mm_vas_create failed");
  167. }
  168. /**
  169. * ps3_mm_vas_destroy -
  170. */
  171. void ps3_mm_vas_destroy(void)
  172. {
  173. int result;
  174. DBG("%s:%d: map.vas_id = %lu\n", __func__, __LINE__, map.vas_id);
  175. if (map.vas_id) {
  176. result = lv1_select_virtual_address_space(0);
  177. BUG_ON(result);
  178. result = lv1_destruct_virtual_address_space(map.vas_id);
  179. BUG_ON(result);
  180. map.vas_id = 0;
  181. }
  182. }
  183. /*============================================================================*/
  184. /* memory hotplug routines */
  185. /*============================================================================*/
  186. /**
  187. * ps3_mm_region_create - create a memory region in the vas
  188. * @r: pointer to a struct mem_region to accept initialized values
  189. * @size: requested region size
  190. *
  191. * This implementation creates the region with the vas large page size.
  192. * @size is rounded down to a multiple of the vas large page size.
  193. */
  194. static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
  195. {
  196. int result;
  197. unsigned long muid;
  198. r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
  199. DBG("%s:%d requested %lxh\n", __func__, __LINE__, size);
  200. DBG("%s:%d actual %lxh\n", __func__, __LINE__, r->size);
  201. DBG("%s:%d difference %lxh (%luMB)\n", __func__, __LINE__,
  202. (unsigned long)(size - r->size),
  203. (size - r->size) / 1024 / 1024);
  204. if (r->size == 0) {
  205. DBG("%s:%d: size == 0\n", __func__, __LINE__);
  206. result = -1;
  207. goto zero_region;
  208. }
  209. result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
  210. ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
  211. if (result || r->base < map.rm.size) {
  212. DBG("%s:%d: lv1_allocate_memory failed: %s\n",
  213. __func__, __LINE__, ps3_result(result));
  214. goto zero_region;
  215. }
  216. r->offset = r->base - map.rm.size;
  217. return result;
  218. zero_region:
  219. r->size = r->base = r->offset = 0;
  220. return result;
  221. }
  222. /**
  223. * ps3_mm_region_destroy - destroy a memory region
  224. * @r: pointer to struct mem_region
  225. */
  226. static void ps3_mm_region_destroy(struct mem_region *r)
  227. {
  228. int result;
  229. DBG("%s:%d: r->base = %lxh\n", __func__, __LINE__, r->base);
  230. if (r->base) {
  231. result = lv1_release_memory(r->base);
  232. BUG_ON(result);
  233. r->size = r->base = r->offset = 0;
  234. map.total = map.rm.size;
  235. }
  236. }
  237. /**
  238. * ps3_mm_add_memory - hot add memory
  239. */
  240. static int __init ps3_mm_add_memory(void)
  241. {
  242. int result;
  243. unsigned long start_addr;
  244. unsigned long start_pfn;
  245. unsigned long nr_pages;
  246. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  247. return -ENODEV;
  248. BUG_ON(!mem_init_done);
  249. start_addr = map.rm.size;
  250. start_pfn = start_addr >> PAGE_SHIFT;
  251. nr_pages = (map.r1.size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  252. DBG("%s:%d: start_addr %lxh, start_pfn %lxh, nr_pages %lxh\n",
  253. __func__, __LINE__, start_addr, start_pfn, nr_pages);
  254. result = add_memory(0, start_addr, map.r1.size);
  255. if (result) {
  256. DBG("%s:%d: add_memory failed: (%d)\n",
  257. __func__, __LINE__, result);
  258. return result;
  259. }
  260. result = online_pages(start_pfn, nr_pages);
  261. if (result)
  262. DBG("%s:%d: online_pages failed: (%d)\n",
  263. __func__, __LINE__, result);
  264. return result;
  265. }
  266. core_initcall(ps3_mm_add_memory);
  267. /*============================================================================*/
  268. /* dma routines */
  269. /*============================================================================*/
  270. /**
  271. * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
  272. * @r: pointer to dma region structure
  273. * @lpar_addr: HV lpar address
  274. */
  275. static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
  276. unsigned long lpar_addr)
  277. {
  278. if (lpar_addr >= map.rm.size)
  279. lpar_addr -= map.r1.offset;
  280. BUG_ON(lpar_addr < r->offset);
  281. BUG_ON(lpar_addr >= r->offset + r->len);
  282. return r->bus_addr + lpar_addr - r->offset;
  283. }
  284. #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
  285. static void __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
  286. const char *func, int line)
  287. {
  288. DBG("%s:%d: dev %lu:%lu\n", func, line, r->dev->bus_id,
  289. r->dev->dev_id);
  290. DBG("%s:%d: page_size %u\n", func, line, r->page_size);
  291. DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
  292. DBG("%s:%d: len %lxh\n", func, line, r->len);
  293. DBG("%s:%d: offset %lxh\n", func, line, r->offset);
  294. }
  295. /**
  296. * dma_chunk - A chunk of dma pages mapped by the io controller.
  297. * @region - The dma region that owns this chunk.
  298. * @lpar_addr: Starting lpar address of the area to map.
  299. * @bus_addr: Starting ioc bus address of the area to map.
  300. * @len: Length in bytes of the area to map.
  301. * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
  302. * list of all chuncks owned by the region.
  303. *
  304. * This implementation uses a very simple dma page manager
  305. * based on the dma_chunk structure. This scheme assumes
  306. * that all drivers use very well behaved dma ops.
  307. */
  308. struct dma_chunk {
  309. struct ps3_dma_region *region;
  310. unsigned long lpar_addr;
  311. unsigned long bus_addr;
  312. unsigned long len;
  313. struct list_head link;
  314. unsigned int usage_count;
  315. };
  316. #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
  317. static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
  318. int line)
  319. {
  320. DBG("%s:%d: r.dev %lu:%lu\n", func, line,
  321. c->region->dev->bus_id, c->region->dev->dev_id);
  322. DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr);
  323. DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size);
  324. DBG("%s:%d: r.len %lxh\n", func, line, c->region->len);
  325. DBG("%s:%d: r.offset %lxh\n", func, line, c->region->offset);
  326. DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr);
  327. DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr);
  328. DBG("%s:%d: c.len %lxh\n", func, line, c->len);
  329. }
  330. static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
  331. unsigned long bus_addr, unsigned long len)
  332. {
  333. struct dma_chunk *c;
  334. unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 1 << r->page_size);
  335. unsigned long aligned_len = _ALIGN_UP(len+bus_addr-aligned_bus,
  336. 1 << r->page_size);
  337. list_for_each_entry(c, &r->chunk_list.head, link) {
  338. /* intersection */
  339. if (aligned_bus >= c->bus_addr &&
  340. aligned_bus + aligned_len <= c->bus_addr + c->len)
  341. return c;
  342. /* below */
  343. if (aligned_bus + aligned_len <= c->bus_addr)
  344. continue;
  345. /* above */
  346. if (aligned_bus >= c->bus_addr + c->len)
  347. continue;
  348. /* we don't handle the multi-chunk case for now */
  349. dma_dump_chunk(c);
  350. BUG();
  351. }
  352. return NULL;
  353. }
  354. static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
  355. unsigned long lpar_addr, unsigned long len)
  356. {
  357. struct dma_chunk *c;
  358. unsigned long aligned_lpar = _ALIGN_DOWN(lpar_addr, 1 << r->page_size);
  359. unsigned long aligned_len = _ALIGN_UP(len + lpar_addr - aligned_lpar,
  360. 1 << r->page_size);
  361. list_for_each_entry(c, &r->chunk_list.head, link) {
  362. /* intersection */
  363. if (c->lpar_addr <= aligned_lpar &&
  364. aligned_lpar < c->lpar_addr + c->len) {
  365. if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
  366. return c;
  367. else {
  368. dma_dump_chunk(c);
  369. BUG();
  370. }
  371. }
  372. /* below */
  373. if (aligned_lpar + aligned_len <= c->lpar_addr) {
  374. continue;
  375. }
  376. /* above */
  377. if (c->lpar_addr + c->len <= aligned_lpar) {
  378. continue;
  379. }
  380. }
  381. return NULL;
  382. }
  383. static int dma_sb_free_chunk(struct dma_chunk *c)
  384. {
  385. int result = 0;
  386. if (c->bus_addr) {
  387. result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
  388. c->region->dev->dev_id, c->bus_addr, c->len);
  389. BUG_ON(result);
  390. }
  391. kfree(c);
  392. return result;
  393. }
  394. static int dma_ioc0_free_chunk(struct dma_chunk *c)
  395. {
  396. int result = 0;
  397. int iopage;
  398. unsigned long offset;
  399. struct ps3_dma_region *r = c->region;
  400. DBG("%s:start\n", __func__);
  401. for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
  402. offset = (1 << r->page_size) * iopage;
  403. /* put INVALID entry */
  404. result = lv1_put_iopte(0,
  405. c->bus_addr + offset,
  406. c->lpar_addr + offset,
  407. r->ioid,
  408. 0);
  409. DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
  410. c->bus_addr + offset,
  411. c->lpar_addr + offset,
  412. r->ioid);
  413. if (result) {
  414. DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
  415. __LINE__, ps3_result(result));
  416. }
  417. }
  418. kfree(c);
  419. DBG("%s:end\n", __func__);
  420. return result;
  421. }
  422. /**
  423. * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
  424. * @r: Pointer to a struct ps3_dma_region.
  425. * @phys_addr: Starting physical address of the area to map.
  426. * @len: Length in bytes of the area to map.
  427. * c_out: A pointer to receive an allocated struct dma_chunk for this area.
  428. *
  429. * This is the lowest level dma mapping routine, and is the one that will
  430. * make the HV call to add the pages into the io controller address space.
  431. */
  432. static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
  433. unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
  434. {
  435. int result;
  436. struct dma_chunk *c;
  437. c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
  438. if (!c) {
  439. result = -ENOMEM;
  440. goto fail_alloc;
  441. }
  442. c->region = r;
  443. c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
  444. c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
  445. c->len = len;
  446. BUG_ON(iopte_flag != 0xf800000000000000UL);
  447. result = lv1_map_device_dma_region(c->region->dev->bus_id,
  448. c->region->dev->dev_id, c->lpar_addr,
  449. c->bus_addr, c->len, iopte_flag);
  450. if (result) {
  451. DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
  452. __func__, __LINE__, ps3_result(result));
  453. goto fail_map;
  454. }
  455. list_add(&c->link, &r->chunk_list.head);
  456. *c_out = c;
  457. return 0;
  458. fail_map:
  459. kfree(c);
  460. fail_alloc:
  461. *c_out = NULL;
  462. DBG(" <- %s:%d\n", __func__, __LINE__);
  463. return result;
  464. }
  465. static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
  466. unsigned long len, struct dma_chunk **c_out,
  467. u64 iopte_flag)
  468. {
  469. int result;
  470. struct dma_chunk *c, *last;
  471. int iopage, pages;
  472. unsigned long offset;
  473. DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
  474. phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
  475. c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
  476. if (!c) {
  477. result = -ENOMEM;
  478. goto fail_alloc;
  479. }
  480. c->region = r;
  481. c->len = len;
  482. c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
  483. /* allocate IO address */
  484. if (list_empty(&r->chunk_list.head)) {
  485. /* first one */
  486. c->bus_addr = r->bus_addr;
  487. } else {
  488. /* derive from last bus addr*/
  489. last = list_entry(r->chunk_list.head.next,
  490. struct dma_chunk, link);
  491. c->bus_addr = last->bus_addr + last->len;
  492. DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
  493. last->bus_addr, last->len);
  494. }
  495. /* FIXME: check whether length exceeds region size */
  496. /* build ioptes for the area */
  497. pages = len >> r->page_size;
  498. DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#lx\n", __func__,
  499. r->page_size, r->len, pages, iopte_flag);
  500. for (iopage = 0; iopage < pages; iopage++) {
  501. offset = (1 << r->page_size) * iopage;
  502. result = lv1_put_iopte(0,
  503. c->bus_addr + offset,
  504. c->lpar_addr + offset,
  505. r->ioid,
  506. iopte_flag);
  507. if (result) {
  508. printk(KERN_WARNING "%s:%d: lv1_map_device_dma_region "
  509. "failed: %s\n", __func__, __LINE__,
  510. ps3_result(result));
  511. goto fail_map;
  512. }
  513. DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
  514. iopage, c->bus_addr + offset, c->lpar_addr + offset,
  515. r->ioid);
  516. }
  517. /* be sure that last allocated one is inserted at head */
  518. list_add(&c->link, &r->chunk_list.head);
  519. *c_out = c;
  520. DBG("%s: end\n", __func__);
  521. return 0;
  522. fail_map:
  523. for (iopage--; 0 <= iopage; iopage--) {
  524. lv1_put_iopte(0,
  525. c->bus_addr + offset,
  526. c->lpar_addr + offset,
  527. r->ioid,
  528. 0);
  529. }
  530. kfree(c);
  531. fail_alloc:
  532. *c_out = NULL;
  533. return result;
  534. }
  535. /**
  536. * dma_sb_region_create - Create a device dma region.
  537. * @r: Pointer to a struct ps3_dma_region.
  538. *
  539. * This is the lowest level dma region create routine, and is the one that
  540. * will make the HV call to create the region.
  541. */
  542. static int dma_sb_region_create(struct ps3_dma_region *r)
  543. {
  544. int result;
  545. pr_info(" -> %s:%d:\n", __func__, __LINE__);
  546. BUG_ON(!r);
  547. if (!r->dev->bus_id) {
  548. pr_info("%s:%d: %lu:%lu no dma\n", __func__, __LINE__,
  549. r->dev->bus_id, r->dev->dev_id);
  550. return 0;
  551. }
  552. DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
  553. __LINE__, r->len, r->page_size, r->offset);
  554. BUG_ON(!r->len);
  555. BUG_ON(!r->page_size);
  556. BUG_ON(!r->region_ops);
  557. INIT_LIST_HEAD(&r->chunk_list.head);
  558. spin_lock_init(&r->chunk_list.lock);
  559. result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
  560. roundup_pow_of_two(r->len), r->page_size, r->region_type,
  561. &r->bus_addr);
  562. if (result) {
  563. DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
  564. __func__, __LINE__, ps3_result(result));
  565. r->len = r->bus_addr = 0;
  566. }
  567. return result;
  568. }
  569. static int dma_ioc0_region_create(struct ps3_dma_region *r)
  570. {
  571. int result;
  572. INIT_LIST_HEAD(&r->chunk_list.head);
  573. spin_lock_init(&r->chunk_list.lock);
  574. result = lv1_allocate_io_segment(0,
  575. r->len,
  576. r->page_size,
  577. &r->bus_addr);
  578. if (result) {
  579. DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
  580. __func__, __LINE__, ps3_result(result));
  581. r->len = r->bus_addr = 0;
  582. }
  583. DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
  584. r->len, r->page_size, r->bus_addr);
  585. return result;
  586. }
  587. /**
  588. * dma_region_free - Free a device dma region.
  589. * @r: Pointer to a struct ps3_dma_region.
  590. *
  591. * This is the lowest level dma region free routine, and is the one that
  592. * will make the HV call to free the region.
  593. */
  594. static int dma_sb_region_free(struct ps3_dma_region *r)
  595. {
  596. int result;
  597. struct dma_chunk *c;
  598. struct dma_chunk *tmp;
  599. BUG_ON(!r);
  600. if (!r->dev->bus_id) {
  601. pr_info("%s:%d: %lu:%lu no dma\n", __func__, __LINE__,
  602. r->dev->bus_id, r->dev->dev_id);
  603. return 0;
  604. }
  605. list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
  606. list_del(&c->link);
  607. dma_sb_free_chunk(c);
  608. }
  609. result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
  610. r->bus_addr);
  611. if (result)
  612. DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
  613. __func__, __LINE__, ps3_result(result));
  614. r->bus_addr = 0;
  615. return result;
  616. }
  617. static int dma_ioc0_region_free(struct ps3_dma_region *r)
  618. {
  619. int result;
  620. struct dma_chunk *c, *n;
  621. DBG("%s: start\n", __func__);
  622. list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
  623. list_del(&c->link);
  624. dma_ioc0_free_chunk(c);
  625. }
  626. result = lv1_release_io_segment(0, r->bus_addr);
  627. if (result)
  628. DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
  629. __func__, __LINE__, ps3_result(result));
  630. r->bus_addr = 0;
  631. DBG("%s: end\n", __func__);
  632. return result;
  633. }
  634. /**
  635. * dma_sb_map_area - Map an area of memory into a device dma region.
  636. * @r: Pointer to a struct ps3_dma_region.
  637. * @virt_addr: Starting virtual address of the area to map.
  638. * @len: Length in bytes of the area to map.
  639. * @bus_addr: A pointer to return the starting ioc bus address of the area to
  640. * map.
  641. *
  642. * This is the common dma mapping routine.
  643. */
  644. static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
  645. unsigned long len, unsigned long *bus_addr,
  646. u64 iopte_flag)
  647. {
  648. int result;
  649. unsigned long flags;
  650. struct dma_chunk *c;
  651. unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
  652. : virt_addr;
  653. unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
  654. unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
  655. 1 << r->page_size);
  656. *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
  657. if (!USE_DYNAMIC_DMA) {
  658. unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
  659. DBG(" -> %s:%d\n", __func__, __LINE__);
  660. DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
  661. virt_addr);
  662. DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
  663. phys_addr);
  664. DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
  665. lpar_addr);
  666. DBG("%s:%d len %lxh\n", __func__, __LINE__, len);
  667. DBG("%s:%d bus_addr %lxh (%lxh)\n", __func__, __LINE__,
  668. *bus_addr, len);
  669. }
  670. spin_lock_irqsave(&r->chunk_list.lock, flags);
  671. c = dma_find_chunk(r, *bus_addr, len);
  672. if (c) {
  673. DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
  674. dma_dump_chunk(c);
  675. c->usage_count++;
  676. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  677. return 0;
  678. }
  679. result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
  680. if (result) {
  681. *bus_addr = 0;
  682. DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
  683. __func__, __LINE__, result);
  684. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  685. return result;
  686. }
  687. c->usage_count = 1;
  688. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  689. return result;
  690. }
  691. static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
  692. unsigned long len, unsigned long *bus_addr,
  693. u64 iopte_flag)
  694. {
  695. int result;
  696. unsigned long flags;
  697. struct dma_chunk *c;
  698. unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
  699. : virt_addr;
  700. unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
  701. unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
  702. 1 << r->page_size);
  703. DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
  704. virt_addr, len);
  705. DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
  706. phys_addr, aligned_phys, aligned_len);
  707. spin_lock_irqsave(&r->chunk_list.lock, flags);
  708. c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
  709. if (c) {
  710. /* FIXME */
  711. BUG();
  712. *bus_addr = c->bus_addr + phys_addr - aligned_phys;
  713. c->usage_count++;
  714. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  715. return 0;
  716. }
  717. result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
  718. iopte_flag);
  719. if (result) {
  720. *bus_addr = 0;
  721. DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
  722. __func__, __LINE__, result);
  723. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  724. return result;
  725. }
  726. *bus_addr = c->bus_addr + phys_addr - aligned_phys;
  727. DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#lx\n", __func__,
  728. virt_addr, phys_addr, aligned_phys, *bus_addr);
  729. c->usage_count = 1;
  730. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  731. return result;
  732. }
  733. /**
  734. * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
  735. * @r: Pointer to a struct ps3_dma_region.
  736. * @bus_addr: The starting ioc bus address of the area to unmap.
  737. * @len: Length in bytes of the area to unmap.
  738. *
  739. * This is the common dma unmap routine.
  740. */
  741. static int dma_sb_unmap_area(struct ps3_dma_region *r, unsigned long bus_addr,
  742. unsigned long len)
  743. {
  744. unsigned long flags;
  745. struct dma_chunk *c;
  746. spin_lock_irqsave(&r->chunk_list.lock, flags);
  747. c = dma_find_chunk(r, bus_addr, len);
  748. if (!c) {
  749. unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
  750. 1 << r->page_size);
  751. unsigned long aligned_len = _ALIGN_UP(len + bus_addr
  752. - aligned_bus, 1 << r->page_size);
  753. DBG("%s:%d: not found: bus_addr %lxh\n",
  754. __func__, __LINE__, bus_addr);
  755. DBG("%s:%d: not found: len %lxh\n",
  756. __func__, __LINE__, len);
  757. DBG("%s:%d: not found: aligned_bus %lxh\n",
  758. __func__, __LINE__, aligned_bus);
  759. DBG("%s:%d: not found: aligned_len %lxh\n",
  760. __func__, __LINE__, aligned_len);
  761. BUG();
  762. }
  763. c->usage_count--;
  764. if (!c->usage_count) {
  765. list_del(&c->link);
  766. dma_sb_free_chunk(c);
  767. }
  768. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  769. return 0;
  770. }
  771. static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
  772. unsigned long bus_addr, unsigned long len)
  773. {
  774. unsigned long flags;
  775. struct dma_chunk *c;
  776. DBG("%s: start a=%#lx l=%#lx\n", __func__, bus_addr, len);
  777. spin_lock_irqsave(&r->chunk_list.lock, flags);
  778. c = dma_find_chunk(r, bus_addr, len);
  779. if (!c) {
  780. unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
  781. 1 << r->page_size);
  782. unsigned long aligned_len = _ALIGN_UP(len + bus_addr
  783. - aligned_bus,
  784. 1 << r->page_size);
  785. DBG("%s:%d: not found: bus_addr %lxh\n",
  786. __func__, __LINE__, bus_addr);
  787. DBG("%s:%d: not found: len %lxh\n",
  788. __func__, __LINE__, len);
  789. DBG("%s:%d: not found: aligned_bus %lxh\n",
  790. __func__, __LINE__, aligned_bus);
  791. DBG("%s:%d: not found: aligned_len %lxh\n",
  792. __func__, __LINE__, aligned_len);
  793. BUG();
  794. }
  795. c->usage_count--;
  796. if (!c->usage_count) {
  797. list_del(&c->link);
  798. dma_ioc0_free_chunk(c);
  799. }
  800. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  801. DBG("%s: end\n", __func__);
  802. return 0;
  803. }
  804. /**
  805. * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
  806. * @r: Pointer to a struct ps3_dma_region.
  807. *
  808. * This routine creates an HV dma region for the device and maps all available
  809. * ram into the io controller bus address space.
  810. */
  811. static int dma_sb_region_create_linear(struct ps3_dma_region *r)
  812. {
  813. int result;
  814. unsigned long virt_addr, len, tmp;
  815. if (r->len > 16*1024*1024) { /* FIXME: need proper fix */
  816. /* force 16M dma pages for linear mapping */
  817. if (r->page_size != PS3_DMA_16M) {
  818. pr_info("%s:%d: forcing 16M pages for linear map\n",
  819. __func__, __LINE__);
  820. r->page_size = PS3_DMA_16M;
  821. r->len = _ALIGN_UP(r->len, 1 << r->page_size);
  822. }
  823. }
  824. result = dma_sb_region_create(r);
  825. BUG_ON(result);
  826. if (r->offset < map.rm.size) {
  827. /* Map (part of) 1st RAM chunk */
  828. virt_addr = map.rm.base + r->offset;
  829. len = map.rm.size - r->offset;
  830. if (len > r->len)
  831. len = r->len;
  832. result = dma_sb_map_area(r, virt_addr, len, &tmp,
  833. IOPTE_PP_W | IOPTE_PP_R | IOPTE_SO_RW | IOPTE_M);
  834. BUG_ON(result);
  835. }
  836. if (r->offset + r->len > map.rm.size) {
  837. /* Map (part of) 2nd RAM chunk */
  838. virt_addr = map.rm.size;
  839. len = r->len;
  840. if (r->offset >= map.rm.size)
  841. virt_addr += r->offset - map.rm.size;
  842. else
  843. len -= map.rm.size - r->offset;
  844. result = dma_sb_map_area(r, virt_addr, len, &tmp,
  845. IOPTE_PP_W | IOPTE_PP_R | IOPTE_SO_RW | IOPTE_M);
  846. BUG_ON(result);
  847. }
  848. return result;
  849. }
  850. /**
  851. * dma_sb_region_free_linear - Free a linear dma mapping for a device.
  852. * @r: Pointer to a struct ps3_dma_region.
  853. *
  854. * This routine will unmap all mapped areas and free the HV dma region.
  855. */
  856. static int dma_sb_region_free_linear(struct ps3_dma_region *r)
  857. {
  858. int result;
  859. unsigned long bus_addr, len, lpar_addr;
  860. if (r->offset < map.rm.size) {
  861. /* Unmap (part of) 1st RAM chunk */
  862. lpar_addr = map.rm.base + r->offset;
  863. len = map.rm.size - r->offset;
  864. if (len > r->len)
  865. len = r->len;
  866. bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
  867. result = dma_sb_unmap_area(r, bus_addr, len);
  868. BUG_ON(result);
  869. }
  870. if (r->offset + r->len > map.rm.size) {
  871. /* Unmap (part of) 2nd RAM chunk */
  872. lpar_addr = map.r1.base;
  873. len = r->len;
  874. if (r->offset >= map.rm.size)
  875. lpar_addr += r->offset - map.rm.size;
  876. else
  877. len -= map.rm.size - r->offset;
  878. bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
  879. result = dma_sb_unmap_area(r, bus_addr, len);
  880. BUG_ON(result);
  881. }
  882. result = dma_sb_region_free(r);
  883. BUG_ON(result);
  884. return result;
  885. }
  886. /**
  887. * dma_sb_map_area_linear - Map an area of memory into a device dma region.
  888. * @r: Pointer to a struct ps3_dma_region.
  889. * @virt_addr: Starting virtual address of the area to map.
  890. * @len: Length in bytes of the area to map.
  891. * @bus_addr: A pointer to return the starting ioc bus address of the area to
  892. * map.
  893. *
  894. * This routine just returns the corresponding bus address. Actual mapping
  895. * occurs in dma_region_create_linear().
  896. */
  897. static int dma_sb_map_area_linear(struct ps3_dma_region *r,
  898. unsigned long virt_addr, unsigned long len, unsigned long *bus_addr,
  899. u64 iopte_flag)
  900. {
  901. unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
  902. : virt_addr;
  903. *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
  904. return 0;
  905. }
  906. /**
  907. * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
  908. * @r: Pointer to a struct ps3_dma_region.
  909. * @bus_addr: The starting ioc bus address of the area to unmap.
  910. * @len: Length in bytes of the area to unmap.
  911. *
  912. * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear().
  913. */
  914. static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
  915. unsigned long bus_addr, unsigned long len)
  916. {
  917. return 0;
  918. };
  919. static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = {
  920. .create = dma_sb_region_create,
  921. .free = dma_sb_region_free,
  922. .map = dma_sb_map_area,
  923. .unmap = dma_sb_unmap_area
  924. };
  925. static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
  926. .create = dma_sb_region_create_linear,
  927. .free = dma_sb_region_free_linear,
  928. .map = dma_sb_map_area_linear,
  929. .unmap = dma_sb_unmap_area_linear
  930. };
  931. static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
  932. .create = dma_ioc0_region_create,
  933. .free = dma_ioc0_region_free,
  934. .map = dma_ioc0_map_area,
  935. .unmap = dma_ioc0_unmap_area
  936. };
  937. int ps3_dma_region_init(struct ps3_system_bus_device *dev,
  938. struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
  939. enum ps3_dma_region_type region_type, void *addr, unsigned long len)
  940. {
  941. unsigned long lpar_addr;
  942. lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
  943. r->dev = dev;
  944. r->page_size = page_size;
  945. r->region_type = region_type;
  946. r->offset = lpar_addr;
  947. if (r->offset >= map.rm.size)
  948. r->offset -= map.r1.offset;
  949. r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);
  950. switch (dev->dev_type) {
  951. case PS3_DEVICE_TYPE_SB:
  952. r->region_ops = (USE_DYNAMIC_DMA)
  953. ? &ps3_dma_sb_region_ops
  954. : &ps3_dma_sb_region_linear_ops;
  955. break;
  956. case PS3_DEVICE_TYPE_IOC0:
  957. r->region_ops = &ps3_dma_ioc0_region_ops;
  958. break;
  959. default:
  960. BUG();
  961. return -EINVAL;
  962. }
  963. return 0;
  964. }
  965. EXPORT_SYMBOL(ps3_dma_region_init);
  966. int ps3_dma_region_create(struct ps3_dma_region *r)
  967. {
  968. BUG_ON(!r);
  969. BUG_ON(!r->region_ops);
  970. BUG_ON(!r->region_ops->create);
  971. return r->region_ops->create(r);
  972. }
  973. EXPORT_SYMBOL(ps3_dma_region_create);
  974. int ps3_dma_region_free(struct ps3_dma_region *r)
  975. {
  976. BUG_ON(!r);
  977. BUG_ON(!r->region_ops);
  978. BUG_ON(!r->region_ops->free);
  979. return r->region_ops->free(r);
  980. }
  981. EXPORT_SYMBOL(ps3_dma_region_free);
  982. int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
  983. unsigned long len, unsigned long *bus_addr,
  984. u64 iopte_flag)
  985. {
  986. return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
  987. }
  988. int ps3_dma_unmap(struct ps3_dma_region *r, unsigned long bus_addr,
  989. unsigned long len)
  990. {
  991. return r->region_ops->unmap(r, bus_addr, len);
  992. }
  993. /*============================================================================*/
  994. /* system startup routines */
  995. /*============================================================================*/
  996. /**
  997. * ps3_mm_init - initialize the address space state variables
  998. */
  999. void __init ps3_mm_init(void)
  1000. {
  1001. int result;
  1002. DBG(" -> %s:%d\n", __func__, __LINE__);
  1003. result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
  1004. &map.total);
  1005. if (result)
  1006. panic("ps3_repository_read_mm_info() failed");
  1007. map.rm.offset = map.rm.base;
  1008. map.vas_id = map.htab_size = 0;
  1009. /* this implementation assumes map.rm.base is zero */
  1010. BUG_ON(map.rm.base);
  1011. BUG_ON(!map.rm.size);
  1012. /* arrange to do this in ps3_mm_add_memory */
  1013. ps3_mm_region_create(&map.r1, map.total - map.rm.size);
  1014. /* correct map.total for the real total amount of memory we use */
  1015. map.total = map.rm.size + map.r1.size;
  1016. DBG(" <- %s:%d\n", __func__, __LINE__);
  1017. }
  1018. /**
  1019. * ps3_mm_shutdown - final cleanup of address space
  1020. */
  1021. void ps3_mm_shutdown(void)
  1022. {
  1023. ps3_mm_region_destroy(&map.r1);
  1024. }