Lines Matching +full:msm8974 +full:- +full:ocmem

1 // SPDX-License-Identifier: GPL-2.0-only
3 * The On Chip Memory (OCMEM) allocator allows various clients to allocate
4 * memory from OCMEM based on performance, latency and power requirements.
23 #include <soc/qcom/ocmem.h>
52 struct ocmem { struct
104 static inline void ocmem_write(struct ocmem *ocmem, u32 reg, u32 data) in ocmem_write() argument
106 writel(data, ocmem->mmio + reg); in ocmem_write()
109 static inline u32 ocmem_read(struct ocmem *ocmem, u32 reg) in ocmem_read() argument
111 return readl(ocmem->mmio + reg); in ocmem_read()
114 static void update_ocmem(struct ocmem *ocmem) in update_ocmem() argument
120 for (i = 0; i < ocmem->config->num_regions; i++) { in update_ocmem()
121 struct ocmem_region *region = &ocmem->regions[i]; in update_ocmem()
123 if (region->mode == THIN_MODE) in update_ocmem()
127 dev_dbg(ocmem->dev, "ocmem_region_mode_control %x\n", in update_ocmem()
129 ocmem_write(ocmem, OCMEM_REG_REGION_MODE_CTL, region_mode_ctrl); in update_ocmem()
132 for (i = 0; i < ocmem->config->num_regions; i++) { in update_ocmem()
133 struct ocmem_region *region = &ocmem->regions[i]; in update_ocmem()
136 data = OCMEM_PSGSC_CTL_MACRO0_MODE(region->macro_state[0]) | in update_ocmem()
137 OCMEM_PSGSC_CTL_MACRO1_MODE(region->macro_state[1]) | in update_ocmem()
138 OCMEM_PSGSC_CTL_MACRO2_MODE(region->macro_state[2]) | in update_ocmem()
139 OCMEM_PSGSC_CTL_MACRO3_MODE(region->macro_state[3]); in update_ocmem()
141 ocmem_write(ocmem, OCMEM_REG_PSGSC_CTL(i), data); in update_ocmem()
145 static unsigned long phys_to_offset(struct ocmem *ocmem, in phys_to_offset() argument
148 if (addr < ocmem->memory->start || addr >= ocmem->memory->end) in phys_to_offset()
151 return addr - ocmem->memory->start; in phys_to_offset()
154 static unsigned long device_address(struct ocmem *ocmem, in device_address() argument
161 return phys_to_offset(ocmem, addr); in device_address()
164 static void update_range(struct ocmem *ocmem, struct ocmem_buf *buf, in update_range() argument
170 for (i = 0; i < ocmem->config->num_regions; i++) { in update_range()
171 struct ocmem_region *region = &ocmem->regions[i]; in update_range()
173 if (buf->offset <= offset && offset < buf->offset + buf->len) in update_range()
174 region->mode = rmode; in update_range()
176 for (j = 0; j < region->num_macros; j++) { in update_range()
177 if (buf->offset <= offset && in update_range()
178 offset < buf->offset + buf->len) in update_range()
179 region->macro_state[j] = mstate; in update_range()
181 offset += region->macro_size; in update_range()
185 update_ocmem(ocmem); in update_range()
188 struct ocmem *of_get_ocmem(struct device *dev) in of_get_ocmem()
193 devnode = of_parse_phandle(dev->of_node, "sram", 0); in of_get_ocmem()
194 if (!devnode || !devnode->parent) { in of_get_ocmem()
196 return ERR_PTR(-ENODEV); in of_get_ocmem()
199 pdev = of_find_device_by_node(devnode->parent); in of_get_ocmem()
201 dev_err(dev, "Cannot find device node %s\n", devnode->name); in of_get_ocmem()
202 return ERR_PTR(-EPROBE_DEFER); in of_get_ocmem()
209 struct ocmem_buf *ocmem_allocate(struct ocmem *ocmem, enum ocmem_client client, in ocmem_allocate() argument
217 return ERR_PTR(-ENODEV); in ocmem_allocate()
220 return ERR_PTR(-EINVAL); in ocmem_allocate()
222 if (test_and_set_bit_lock(BIT(client), &ocmem->active_allocations)) in ocmem_allocate()
223 return ERR_PTR(-EBUSY); in ocmem_allocate()
227 ret = -ENOMEM; in ocmem_allocate()
231 buf->offset = 0; in ocmem_allocate()
232 buf->addr = device_address(ocmem, client, buf->offset); in ocmem_allocate()
233 buf->len = size; in ocmem_allocate()
235 update_range(ocmem, buf, CORE_ON, WIDE_MODE); in ocmem_allocate()
239 buf->offset, buf->len, WIDE_MODE); in ocmem_allocate()
241 dev_err(ocmem->dev, "could not lock: %d\n", ret); in ocmem_allocate()
242 ret = -EINVAL; in ocmem_allocate()
246 ocmem_write(ocmem, OCMEM_REG_GFX_MPU_START, buf->offset); in ocmem_allocate()
247 ocmem_write(ocmem, OCMEM_REG_GFX_MPU_END, in ocmem_allocate()
248 buf->offset + buf->len); in ocmem_allocate()
251 dev_dbg(ocmem->dev, "using %ldK of OCMEM at 0x%08lx for client %d\n", in ocmem_allocate()
252 size / 1024, buf->addr, client); in ocmem_allocate()
259 clear_bit_unlock(BIT(client), &ocmem->active_allocations); in ocmem_allocate()
265 void ocmem_free(struct ocmem *ocmem, enum ocmem_client client, in ocmem_free() argument
272 update_range(ocmem, buf, CLK_OFF, MODE_DEFAULT); in ocmem_free()
278 buf->offset, buf->len); in ocmem_free()
280 dev_err(ocmem->dev, "could not unlock: %d\n", ret); in ocmem_free()
282 ocmem_write(ocmem, OCMEM_REG_GFX_MPU_START, 0x0); in ocmem_free()
283 ocmem_write(ocmem, OCMEM_REG_GFX_MPU_END, 0x0); in ocmem_free()
288 clear_bit_unlock(BIT(client), &ocmem->active_allocations); in ocmem_free()
294 struct device *dev = &pdev->dev; in ocmem_dev_probe()
298 struct ocmem *ocmem; in ocmem_dev_probe() local
301 return -EPROBE_DEFER; in ocmem_dev_probe()
303 ocmem = devm_kzalloc(dev, sizeof(*ocmem), GFP_KERNEL); in ocmem_dev_probe()
304 if (!ocmem) in ocmem_dev_probe()
305 return -ENOMEM; in ocmem_dev_probe()
307 ocmem->dev = dev; in ocmem_dev_probe()
308 ocmem->config = device_get_match_data(dev); in ocmem_dev_probe()
312 if (ret != -EPROBE_DEFER) in ocmem_dev_probe()
319 ocmem->mmio = devm_ioremap_resource(&pdev->dev, res); in ocmem_dev_probe()
320 if (IS_ERR(ocmem->mmio)) { in ocmem_dev_probe()
321 dev_err(&pdev->dev, "Failed to ioremap ocmem_ctrl resource\n"); in ocmem_dev_probe()
322 return PTR_ERR(ocmem->mmio); in ocmem_dev_probe()
325 ocmem->memory = platform_get_resource_byname(pdev, IORESOURCE_MEM, in ocmem_dev_probe()
327 if (!ocmem->memory) { in ocmem_dev_probe()
329 return -ENXIO; in ocmem_dev_probe()
337 dev_info(ocmem->dev, "Failed to enable clocks\n"); in ocmem_dev_probe()
350 reg = ocmem_read(ocmem, OCMEM_REG_HW_PROFILE); in ocmem_dev_probe()
351 ocmem->num_ports = OCMEM_HW_PROFILE_NUM_PORTS(reg); in ocmem_dev_probe()
352 ocmem->num_macros = OCMEM_HW_PROFILE_NUM_MACROS(reg); in ocmem_dev_probe()
353 ocmem->interleaved = !!(reg & OCMEM_HW_PROFILE_INTERLEAVING); in ocmem_dev_probe()
355 num_banks = ocmem->num_ports / 2; in ocmem_dev_probe()
356 region_size = ocmem->config->macro_size * num_banks; in ocmem_dev_probe()
359 ocmem->num_ports, ocmem->config->num_regions, in ocmem_dev_probe()
360 ocmem->num_macros, ocmem->interleaved ? "" : "not "); in ocmem_dev_probe()
362 ocmem->regions = devm_kcalloc(dev, ocmem->config->num_regions, in ocmem_dev_probe()
364 if (!ocmem->regions) { in ocmem_dev_probe()
365 ret = -ENOMEM; in ocmem_dev_probe()
369 for (i = 0; i < ocmem->config->num_regions; i++) { in ocmem_dev_probe()
370 struct ocmem_region *region = &ocmem->regions[i]; in ocmem_dev_probe()
372 if (WARN_ON(num_banks > ARRAY_SIZE(region->macro_state))) { in ocmem_dev_probe()
373 ret = -EINVAL; in ocmem_dev_probe()
377 region->mode = MODE_DEFAULT; in ocmem_dev_probe()
378 region->num_macros = num_banks; in ocmem_dev_probe()
380 if (i == (ocmem->config->num_regions - 1) && in ocmem_dev_probe()
382 region->macro_size = ocmem->config->macro_size / 2; in ocmem_dev_probe()
383 region->region_size = region_size / 2; in ocmem_dev_probe()
385 region->macro_size = ocmem->config->macro_size; in ocmem_dev_probe()
386 region->region_size = region_size; in ocmem_dev_probe()
389 for (j = 0; j < ARRAY_SIZE(region->macro_state); j++) in ocmem_dev_probe()
390 region->macro_state[j] = CLK_OFF; in ocmem_dev_probe()
393 platform_set_drvdata(pdev, ocmem); in ocmem_dev_probe()
415 { .compatible = "qcom,msm8974-ocmem", .data = &ocmem_8974_config },
425 .name = "ocmem",
432 MODULE_DESCRIPTION("On Chip Memory (OCMEM) allocator for some Snapdragon SoCs");