sq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * arch/sh/kernel/cpu/sq.c
  3. *
  4. * General management API for SH-4 integrated Store Queues
  5. *
  6. * Copyright (C) 2001, 2002, 2003, 2004 Paul Mundt
  7. * Copyright (C) 2001, 2002 M. R. Brown
  8. *
  9. * Some of this code has been adopted directly from the old arch/sh/mm/sq.c
  10. * hack that was part of the LinuxDC project. For all intents and purposes,
  11. * this is a completely new interface that really doesn't have much in common
  12. * with the old zone-based approach at all. In fact, it's only listed here for
  13. * general completeness.
  14. *
  15. * This file is subject to the terms and conditions of the GNU General Public
  16. * License. See the file "COPYING" in the main directory of this archive
  17. * for more details.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/list.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/mm.h>
  28. #include <asm/io.h>
  29. #include <asm/page.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/cpu/sq.h>
  33. static LIST_HEAD(sq_mapping_list);
  34. static DEFINE_SPINLOCK(sq_mapping_lock);
  35. /**
  36. * sq_flush - Flush (prefetch) the store queue cache
  37. * @addr: the store queue address to flush
  38. *
  39. * Executes a prefetch instruction on the specified store queue cache,
  40. * so that the cached data is written to physical memory.
  41. */
  42. inline void sq_flush(void *addr)
  43. {
  44. __asm__ __volatile__ ("pref @%0" : : "r" (addr) : "memory");
  45. }
  46. /**
  47. * sq_flush_range - Flush (prefetch) a specific SQ range
  48. * @start: the store queue address to start flushing from
  49. * @len: the length to flush
  50. *
  51. * Flushes the store queue cache from @start to @start + @len in a
  52. * linear fashion.
  53. */
  54. void sq_flush_range(unsigned long start, unsigned int len)
  55. {
  56. volatile unsigned long *sq = (unsigned long *)start;
  57. unsigned long dummy;
  58. /* Flush the queues */
  59. for (len >>= 5; len--; sq += 8)
  60. sq_flush((void *)sq);
  61. /* Wait for completion */
  62. dummy = ctrl_inl(P4SEG_STORE_QUE);
  63. ctrl_outl(0, P4SEG_STORE_QUE + 0);
  64. ctrl_outl(0, P4SEG_STORE_QUE + 8);
  65. }
  66. static struct sq_mapping *__sq_alloc_mapping(unsigned long virt, unsigned long phys, unsigned long size, const char *name)
  67. {
  68. struct sq_mapping *map;
  69. if (virt + size > SQ_ADDRMAX)
  70. return ERR_PTR(-ENOSPC);
  71. map = kmalloc(sizeof(struct sq_mapping), GFP_KERNEL);
  72. if (!map)
  73. return ERR_PTR(-ENOMEM);
  74. INIT_LIST_HEAD(&map->list);
  75. map->sq_addr = virt;
  76. map->addr = phys;
  77. map->size = size + 1;
  78. map->name = name;
  79. list_add(&map->list, &sq_mapping_list);
  80. return map;
  81. }
  82. static unsigned long __sq_get_next_addr(void)
  83. {
  84. if (!list_empty(&sq_mapping_list)) {
  85. struct list_head *pos, *tmp;
  86. /*
  87. * Read one off the list head, as it will have the highest
  88. * mapped allocation. Set the next one up right above it.
  89. *
  90. * This is somewhat sub-optimal, as we don't look at
  91. * gaps between allocations or anything lower then the
  92. * highest-level allocation.
  93. *
  94. * However, in the interest of performance and the general
  95. * lack of desire to do constant list rebalancing, we don't
  96. * worry about it.
  97. */
  98. list_for_each_safe(pos, tmp, &sq_mapping_list) {
  99. struct sq_mapping *entry;
  100. entry = list_entry(pos, typeof(*entry), list);
  101. return entry->sq_addr + entry->size;
  102. }
  103. }
  104. return P4SEG_STORE_QUE;
  105. }
  106. /**
  107. * __sq_remap - Perform a translation from the SQ to a phys addr
  108. * @map: sq mapping containing phys and store queue addresses.
  109. *
  110. * Maps the store queue address specified in the mapping to the physical
  111. * address specified in the mapping.
  112. */
  113. static struct sq_mapping *__sq_remap(struct sq_mapping *map)
  114. {
  115. unsigned long flags, pteh, ptel;
  116. struct vm_struct *vma;
  117. pgprot_t pgprot;
  118. /*
  119. * Without an MMU (or with it turned off), this is much more
  120. * straightforward, as we can just load up each queue's QACR with
  121. * the physical address appropriately masked.
  122. */
  123. ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR0);
  124. ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR1);
  125. #ifdef CONFIG_MMU
  126. /*
  127. * With an MMU on the other hand, things are slightly more involved.
  128. * Namely, we have to have a direct mapping between the SQ addr and
  129. * the associated physical address in the UTLB by way of setting up
  130. * a virt<->phys translation by hand. We do this by simply specifying
  131. * the SQ addr in UTLB.VPN and the associated physical address in
  132. * UTLB.PPN.
  133. *
  134. * Notably, even though this is a special case translation, and some
  135. * of the configuration bits are meaningless, we're still required
  136. * to have a valid ASID context in PTEH.
  137. *
  138. * We could also probably get by without explicitly setting PTEA, but
  139. * we do it here just for good measure.
  140. */
  141. spin_lock_irqsave(&sq_mapping_lock, flags);
  142. pteh = map->sq_addr;
  143. ctrl_outl((pteh & MMU_VPN_MASK) | get_asid(), MMU_PTEH);
  144. ptel = map->addr & PAGE_MASK;
  145. if (cpu_data->flags & CPU_HAS_PTEA)
  146. ctrl_outl(((ptel >> 28) & 0xe) | (ptel & 0x1), MMU_PTEA);
  147. pgprot = pgprot_noncached(PAGE_KERNEL);
  148. ptel &= _PAGE_FLAGS_HARDWARE_MASK;
  149. ptel |= pgprot_val(pgprot);
  150. ctrl_outl(ptel, MMU_PTEL);
  151. __asm__ __volatile__ ("ldtlb" : : : "memory");
  152. spin_unlock_irqrestore(&sq_mapping_lock, flags);
  153. /*
  154. * Next, we need to map ourselves in the kernel page table, so that
  155. * future accesses after a TLB flush will be handled when we take a
  156. * page fault.
  157. *
  158. * Theoretically we could just do this directly and not worry about
  159. * setting up the translation by hand ahead of time, but for the
  160. * cases where we want a one-shot SQ mapping followed by a quick
  161. * writeout before we hit the TLB flush, we do it anyways. This way
  162. * we at least save ourselves the initial page fault overhead.
  163. */
  164. vma = __get_vm_area(map->size, VM_ALLOC, map->sq_addr, SQ_ADDRMAX);
  165. if (!vma)
  166. return ERR_PTR(-ENOMEM);
  167. vma->phys_addr = map->addr;
  168. if (remap_area_pages((unsigned long)vma->addr, vma->phys_addr,
  169. map->size, pgprot_val(pgprot))) {
  170. vunmap(vma->addr);
  171. return NULL;
  172. }
  173. #endif /* CONFIG_MMU */
  174. return map;
  175. }
  176. /**
  177. * sq_remap - Map a physical address through the Store Queues
  178. * @phys: Physical address of mapping.
  179. * @size: Length of mapping.
  180. * @name: User invoking mapping.
  181. *
  182. * Remaps the physical address @phys through the next available store queue
  183. * address of @size length. @name is logged at boot time as well as through
  184. * the procfs interface.
  185. *
  186. * A pre-allocated and filled sq_mapping pointer is returned, and must be
  187. * cleaned up with a call to sq_unmap() when the user is done with the
  188. * mapping.
  189. */
  190. struct sq_mapping *sq_remap(unsigned long phys, unsigned int size, const char *name)
  191. {
  192. struct sq_mapping *map;
  193. unsigned long virt, end;
  194. unsigned int psz;
  195. /* Don't allow wraparound or zero size */
  196. end = phys + size - 1;
  197. if (!size || end < phys)
  198. return NULL;
  199. /* Don't allow anyone to remap normal memory.. */
  200. if (phys < virt_to_phys(high_memory))
  201. return NULL;
  202. phys &= PAGE_MASK;
  203. size = PAGE_ALIGN(end + 1) - phys;
  204. virt = __sq_get_next_addr();
  205. psz = (size + (PAGE_SIZE - 1)) / PAGE_SIZE;
  206. map = __sq_alloc_mapping(virt, phys, size, name);
  207. printk("sqremap: %15s [%4d page%s] va 0x%08lx pa 0x%08lx\n",
  208. map->name ? map->name : "???",
  209. psz, psz == 1 ? " " : "s",
  210. map->sq_addr, map->addr);
  211. return __sq_remap(map);
  212. }
  213. /**
  214. * sq_unmap - Unmap a Store Queue allocation
  215. * @map: Pre-allocated Store Queue mapping.
  216. *
  217. * Unmaps the store queue allocation @map that was previously created by
  218. * sq_remap(). Also frees up the pte that was previously inserted into
  219. * the kernel page table and discards the UTLB translation.
  220. */
  221. void sq_unmap(struct sq_mapping *map)
  222. {
  223. if (map->sq_addr > (unsigned long)high_memory)
  224. vfree((void *)(map->sq_addr & PAGE_MASK));
  225. list_del(&map->list);
  226. kfree(map);
  227. }
  228. /**
  229. * sq_clear - Clear a store queue range
  230. * @addr: Address to start clearing from.
  231. * @len: Length to clear.
  232. *
  233. * A quick zero-fill implementation for clearing out memory that has been
  234. * remapped through the store queues.
  235. */
  236. void sq_clear(unsigned long addr, unsigned int len)
  237. {
  238. int i;
  239. /* Clear out both queues linearly */
  240. for (i = 0; i < 8; i++) {
  241. ctrl_outl(0, addr + i + 0);
  242. ctrl_outl(0, addr + i + 8);
  243. }
  244. sq_flush_range(addr, len);
  245. }
  246. /**
  247. * sq_vma_unmap - Unmap a VMA range
  248. * @area: VMA containing range.
  249. * @addr: Start of range.
  250. * @len: Length of range.
  251. *
  252. * Searches the sq_mapping_list for a mapping matching the sq addr @addr,
  253. * and subsequently frees up the entry. Further cleanup is done by generic
  254. * code.
  255. */
  256. static void sq_vma_unmap(struct vm_area_struct *area,
  257. unsigned long addr, size_t len)
  258. {
  259. struct list_head *pos, *tmp;
  260. list_for_each_safe(pos, tmp, &sq_mapping_list) {
  261. struct sq_mapping *entry;
  262. entry = list_entry(pos, typeof(*entry), list);
  263. if (entry->sq_addr == addr) {
  264. /*
  265. * We could probably get away without doing the tlb flush
  266. * here, as generic code should take care of most of this
  267. * when unmapping the rest of the VMA range for us. Leave
  268. * it in for added sanity for the time being..
  269. */
  270. __flush_tlb_page(get_asid(), entry->sq_addr & PAGE_MASK);
  271. list_del(&entry->list);
  272. kfree(entry);
  273. return;
  274. }
  275. }
  276. }
  277. /**
  278. * sq_vma_sync - Sync a VMA range
  279. * @area: VMA containing range.
  280. * @start: Start of range.
  281. * @len: Length of range.
  282. * @flags: Additional flags.
  283. *
  284. * Synchronizes an sq mapped range by flushing the store queue cache for
  285. * the duration of the mapping.
  286. *
  287. * Used internally for user mappings, which must use msync() to prefetch
  288. * the store queue cache.
  289. */
  290. static int sq_vma_sync(struct vm_area_struct *area,
  291. unsigned long start, size_t len, unsigned int flags)
  292. {
  293. sq_flush_range(start, len);
  294. return 0;
  295. }
  296. static struct vm_operations_struct sq_vma_ops = {
  297. .unmap = sq_vma_unmap,
  298. .sync = sq_vma_sync,
  299. };
  300. /**
  301. * sq_mmap - mmap() for /dev/cpu/sq
  302. * @file: unused.
  303. * @vma: VMA to remap.
  304. *
  305. * Remap the specified vma @vma through the store queues, and setup associated
  306. * information for the new mapping. Also build up the page tables for the new
  307. * area.
  308. */
  309. static int sq_mmap(struct file *file, struct vm_area_struct *vma)
  310. {
  311. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  312. unsigned long size = vma->vm_end - vma->vm_start;
  313. struct sq_mapping *map;
  314. /*
  315. * We're not interested in any arbitrary virtual address that has
  316. * been stuck in the VMA, as we already know what addresses we
  317. * want. Save off the size, and reposition the VMA to begin at
  318. * the next available sq address.
  319. */
  320. vma->vm_start = __sq_get_next_addr();
  321. vma->vm_end = vma->vm_start + size;
  322. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  323. vma->vm_flags |= VM_IO | VM_RESERVED;
  324. map = __sq_alloc_mapping(vma->vm_start, offset, size, "Userspace");
  325. if (io_remap_pfn_range(vma, map->sq_addr, map->addr >> PAGE_SHIFT,
  326. size, vma->vm_page_prot))
  327. return -EAGAIN;
  328. vma->vm_ops = &sq_vma_ops;
  329. return 0;
  330. }
  331. #ifdef CONFIG_PROC_FS
  332. static int sq_mapping_read_proc(char *buf, char **start, off_t off,
  333. int len, int *eof, void *data)
  334. {
  335. struct list_head *pos;
  336. char *p = buf;
  337. list_for_each_prev(pos, &sq_mapping_list) {
  338. struct sq_mapping *entry;
  339. entry = list_entry(pos, typeof(*entry), list);
  340. p += sprintf(p, "%08lx-%08lx [%08lx]: %s\n", entry->sq_addr,
  341. entry->sq_addr + entry->size - 1, entry->addr,
  342. entry->name);
  343. }
  344. return p - buf;
  345. }
  346. #endif
  347. static struct file_operations sq_fops = {
  348. .owner = THIS_MODULE,
  349. .mmap = sq_mmap,
  350. };
  351. static struct miscdevice sq_dev = {
  352. .minor = STORE_QUEUE_MINOR,
  353. .name = "sq",
  354. .fops = &sq_fops,
  355. };
  356. static int __init sq_api_init(void)
  357. {
  358. int ret;
  359. printk(KERN_NOTICE "sq: Registering store queue API.\n");
  360. create_proc_read_entry("sq_mapping", 0, 0, sq_mapping_read_proc, 0);
  361. ret = misc_register(&sq_dev);
  362. if (ret)
  363. remove_proc_entry("sq_mapping", NULL);
  364. return ret;
  365. }
  366. static void __exit sq_api_exit(void)
  367. {
  368. misc_deregister(&sq_dev);
  369. remove_proc_entry("sq_mapping", NULL);
  370. }
  371. module_init(sq_api_init);
  372. module_exit(sq_api_exit);
  373. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, M. R. Brown <mrbrown@0xd6.org>");
  374. MODULE_DESCRIPTION("Simple API for SH-4 integrated Store Queues");
  375. MODULE_LICENSE("GPL");
  376. MODULE_ALIAS_MISCDEV(STORE_QUEUE_MINOR);
  377. EXPORT_SYMBOL(sq_remap);
  378. EXPORT_SYMBOL(sq_unmap);
  379. EXPORT_SYMBOL(sq_clear);
  380. EXPORT_SYMBOL(sq_flush);
  381. EXPORT_SYMBOL(sq_flush_range);