mm.c 32 KB

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