mm.c 32 KB

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