mm.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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. lmb_add(map.rm.base, map.rm.size);
  1020. lmb_analyze();
  1021. /* arrange to do this in ps3_mm_add_memory */
  1022. ps3_mm_region_create(&map.r1, map.total - map.rm.size);
  1023. /* correct map.total for the real total amount of memory we use */
  1024. map.total = map.rm.size + map.r1.size;
  1025. DBG(" <- %s:%d\n", __func__, __LINE__);
  1026. }
  1027. /**
  1028. * ps3_mm_shutdown - final cleanup of address space
  1029. */
  1030. void ps3_mm_shutdown(void)
  1031. {
  1032. ps3_mm_region_destroy(&map.r1);
  1033. }