mm.c 32 KB

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