2 * SPU local store allocation routines
4 * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
29 #include <asm/spu_csa.h>
34 static int spu_alloc_lscsa_std(struct spu_state
*csa
)
36 struct spu_lscsa
*lscsa
;
39 lscsa
= vzalloc(sizeof(struct spu_lscsa
));
44 /* Set LS pages reserved to allow for user-space mapping. */
45 for (p
= lscsa
->ls
; p
< lscsa
->ls
+ LS_SIZE
; p
+= PAGE_SIZE
)
46 SetPageReserved(vmalloc_to_page(p
));
51 static void spu_free_lscsa_std(struct spu_state
*csa
)
53 /* Clear reserved bit before vfree. */
56 if (csa
->lscsa
== NULL
)
59 for (p
= csa
->lscsa
->ls
; p
< csa
->lscsa
->ls
+ LS_SIZE
; p
+= PAGE_SIZE
)
60 ClearPageReserved(vmalloc_to_page(p
));
65 #ifdef CONFIG_SPU_FS_64K_LS
67 #define SPU_64K_PAGE_SHIFT 16
68 #define SPU_64K_PAGE_ORDER (SPU_64K_PAGE_SHIFT - PAGE_SHIFT)
69 #define SPU_64K_PAGE_COUNT (1ul << SPU_64K_PAGE_ORDER)
71 int spu_alloc_lscsa(struct spu_state
*csa
)
73 struct page
**pgarray
;
77 /* Check availability of 64K pages */
78 if (!spu_64k_pages_available())
81 csa
->use_big_pages
= 1;
83 pr_debug("spu_alloc_lscsa(csa=0x%p), trying to allocate 64K pages\n",
86 /* First try to allocate our 64K pages. We need 5 of them
87 * with the current implementation. In the future, we should try
88 * to separate the lscsa with the actual local store image, thus
89 * allowing us to require only 4 64K pages per context
91 for (i
= 0; i
< SPU_LSCSA_NUM_BIG_PAGES
; i
++) {
92 /* XXX This is likely to fail, we should use a special pool
93 * similar to what hugetlbfs does.
95 csa
->lscsa_pages
[i
] = alloc_pages(GFP_KERNEL
,
97 if (csa
->lscsa_pages
[i
] == NULL
)
101 pr_debug(" success ! creating vmap...\n");
103 /* Now we need to create a vmalloc mapping of these for the kernel
104 * and SPU context switch code to use. Currently, we stick to a
105 * normal kernel vmalloc mapping, which in our case will be 4K
107 n_4k
= SPU_64K_PAGE_COUNT
* SPU_LSCSA_NUM_BIG_PAGES
;
108 pgarray
= kmalloc(sizeof(struct page
*) * n_4k
, GFP_KERNEL
);
111 for (i
= 0; i
< SPU_LSCSA_NUM_BIG_PAGES
; i
++)
112 for (j
= 0; j
< SPU_64K_PAGE_COUNT
; j
++)
113 /* We assume all the struct page's are contiguous
114 * which should be hopefully the case for an order 4
117 pgarray
[i
* SPU_64K_PAGE_COUNT
+ j
] =
118 csa
->lscsa_pages
[i
] + j
;
119 csa
->lscsa
= vmap(pgarray
, n_4k
, VM_USERMAP
, PAGE_KERNEL
);
121 if (csa
->lscsa
== NULL
)
124 memset(csa
->lscsa
, 0, sizeof(struct spu_lscsa
));
126 /* Set LS pages reserved to allow for user-space mapping.
128 * XXX isn't that a bit obsolete ? I think we should just
129 * make sure the page count is high enough. Anyway, won't harm
132 for (p
= csa
->lscsa
->ls
; p
< csa
->lscsa
->ls
+ LS_SIZE
; p
+= PAGE_SIZE
)
133 SetPageReserved(vmalloc_to_page(p
));
135 pr_debug(" all good !\n");
139 pr_debug("spufs: failed to allocate lscsa 64K pages, falling back\n");
141 return spu_alloc_lscsa_std(csa
);
144 void spu_free_lscsa(struct spu_state
*csa
)
149 if (!csa
->use_big_pages
) {
150 spu_free_lscsa_std(csa
);
153 csa
->use_big_pages
= 0;
155 if (csa
->lscsa
== NULL
)
158 for (p
= csa
->lscsa
->ls
; p
< csa
->lscsa
->ls
+ LS_SIZE
; p
+= PAGE_SIZE
)
159 ClearPageReserved(vmalloc_to_page(p
));
166 for (i
= 0; i
< SPU_LSCSA_NUM_BIG_PAGES
; i
++)
167 if (csa
->lscsa_pages
[i
])
168 __free_pages(csa
->lscsa_pages
[i
], SPU_64K_PAGE_ORDER
);
171 #else /* CONFIG_SPU_FS_64K_LS */
173 int spu_alloc_lscsa(struct spu_state
*csa
)
175 return spu_alloc_lscsa_std(csa
);
178 void spu_free_lscsa(struct spu_state
*csa
)
180 spu_free_lscsa_std(csa
);
183 #endif /* !defined(CONFIG_SPU_FS_64K_LS) */