sq.c 12 KB

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