199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * MD primitives supporting placement of module data
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * XXX should check load address/size against memory top.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "libofw.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define READIN_BUF (4 * 1024)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PAGE_SIZE 0x1000
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PAGE_MASK 0x0fff
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MAPMEM_PAGE_INC 16
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeofw_mapmem(vm_offset_t dest, const size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome void *destp, *addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t dlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t resid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t nlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static vm_offset_t last_dest = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static size_t last_len = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome nlen = len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check to see if this region fits in a prior mapping.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Allocations are generally sequential, so only check
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the last one.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dest >= last_dest &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome (dest + len) <= (last_dest + last_len)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Trim area covered by existing mapping, if any
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dest < (last_dest + last_len) && dest >= last_dest) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome nlen -= (last_dest + last_len) - dest;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dest = last_dest + last_len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome destp = (void *)(dest & ~PAGE_MASK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome resid = dest & PAGE_MASK;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * To avoid repeated mappings on small allocations,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * never map anything less than MAPMEM_PAGE_INC pages at a time
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((nlen + resid) < PAGE_SIZE*MAPMEM_PAGE_INC) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome dlen = PAGE_SIZE*MAPMEM_PAGE_INC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome dlen = roundup(nlen + resid, PAGE_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (OF_call_method("claim", memory, 3, 1, destp, dlen, 0, &addr)
199767f8919635c4928607450d9e0abb932109ceToomas Soome == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ofw_mapmem: physical claim failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOMEM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We only do virtual memory management when real_mode is false.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (real_mode == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (OF_call_method("claim", mmu, 3, 1, destp, dlen, 0, &addr)
199767f8919635c4928607450d9e0abb932109ceToomas Soome == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ofw_mapmem: virtual claim failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOMEM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (OF_call_method("map", mmu, 4, 0, destp, destp, dlen, 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ofw_mapmem: map failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOMEM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome last_dest = (vm_offset_t) destp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome last_len = dlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomessize_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomeofw_copyin(const void *src, vm_offset_t dest, const size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ofw_mapmem(dest, len)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ofw_copyin: map error\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(src, (void *)dest, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomessize_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomeofw_copyout(const vm_offset_t src, void *dest, const size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy((void *)src, dest, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomessize_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomeofw_readin(const int fd, vm_offset_t dest, const size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome void *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t resid, chunk, get;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t got;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = dest;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome chunk = min(READIN_BUF, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = malloc(chunk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (buf == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ofw_readin: buf malloc failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ofw_mapmem(dest, len)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ofw_readin: map error\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (resid = len; resid > 0; resid -= got, p += got) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome get = min(chunk, resid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome got = read(fd, buf, get);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (got <= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (got < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ofw_readin: read failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(buf, (void *)p, got);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(len - resid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}