174fe6c29SRuslan Bukin /*
2*85f87cf4SRuslan Bukin * Copyright (c) 2017-2019, Intel Corporation
374fe6c29SRuslan Bukin *
474fe6c29SRuslan Bukin * Redistribution and use in source and binary forms, with or without
574fe6c29SRuslan Bukin * modification, are permitted provided that the following conditions are met:
674fe6c29SRuslan Bukin *
774fe6c29SRuslan Bukin * * Redistributions of source code must retain the above copyright notice,
874fe6c29SRuslan Bukin * this list of conditions and the following disclaimer.
974fe6c29SRuslan Bukin * * Redistributions in binary form must reproduce the above copyright notice,
1074fe6c29SRuslan Bukin * this list of conditions and the following disclaimer in the documentation
1174fe6c29SRuslan Bukin * and/or other materials provided with the distribution.
1274fe6c29SRuslan Bukin * * Neither the name of Intel Corporation nor the names of its contributors
1374fe6c29SRuslan Bukin * may be used to endorse or promote products derived from this software
1474fe6c29SRuslan Bukin * without specific prior written permission.
1574fe6c29SRuslan Bukin *
1674fe6c29SRuslan Bukin * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1774fe6c29SRuslan Bukin * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1874fe6c29SRuslan Bukin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1974fe6c29SRuslan Bukin * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2074fe6c29SRuslan Bukin * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2174fe6c29SRuslan Bukin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2274fe6c29SRuslan Bukin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2374fe6c29SRuslan Bukin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2474fe6c29SRuslan Bukin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2574fe6c29SRuslan Bukin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2674fe6c29SRuslan Bukin * POSSIBILITY OF SUCH DAMAGE.
2774fe6c29SRuslan Bukin */
2874fe6c29SRuslan Bukin
2974fe6c29SRuslan Bukin #include "pt_msec_cache.h"
3074fe6c29SRuslan Bukin #include "pt_section.h"
3174fe6c29SRuslan Bukin #include "pt_image.h"
3274fe6c29SRuslan Bukin
3374fe6c29SRuslan Bukin #include <string.h>
3474fe6c29SRuslan Bukin
3574fe6c29SRuslan Bukin
pt_msec_cache_init(struct pt_msec_cache * cache)3674fe6c29SRuslan Bukin int pt_msec_cache_init(struct pt_msec_cache *cache)
3774fe6c29SRuslan Bukin {
3874fe6c29SRuslan Bukin if (!cache)
3974fe6c29SRuslan Bukin return -pte_internal;
4074fe6c29SRuslan Bukin
4174fe6c29SRuslan Bukin memset(cache, 0, sizeof(*cache));
4274fe6c29SRuslan Bukin
4374fe6c29SRuslan Bukin return 0;
4474fe6c29SRuslan Bukin }
4574fe6c29SRuslan Bukin
pt_msec_cache_fini(struct pt_msec_cache * cache)4674fe6c29SRuslan Bukin void pt_msec_cache_fini(struct pt_msec_cache *cache)
4774fe6c29SRuslan Bukin {
4874fe6c29SRuslan Bukin if (!cache)
4974fe6c29SRuslan Bukin return;
5074fe6c29SRuslan Bukin
5174fe6c29SRuslan Bukin (void) pt_msec_cache_invalidate(cache);
5274fe6c29SRuslan Bukin pt_msec_fini(&cache->msec);
5374fe6c29SRuslan Bukin }
5474fe6c29SRuslan Bukin
pt_msec_cache_invalidate(struct pt_msec_cache * cache)5574fe6c29SRuslan Bukin int pt_msec_cache_invalidate(struct pt_msec_cache *cache)
5674fe6c29SRuslan Bukin {
5774fe6c29SRuslan Bukin struct pt_section *section;
5874fe6c29SRuslan Bukin int errcode;
5974fe6c29SRuslan Bukin
6074fe6c29SRuslan Bukin if (!cache)
6174fe6c29SRuslan Bukin return -pte_internal;
6274fe6c29SRuslan Bukin
6374fe6c29SRuslan Bukin section = pt_msec_section(&cache->msec);
6474fe6c29SRuslan Bukin if (!section)
6574fe6c29SRuslan Bukin return 0;
6674fe6c29SRuslan Bukin
6774fe6c29SRuslan Bukin errcode = pt_section_unmap(section);
6874fe6c29SRuslan Bukin if (errcode < 0)
6974fe6c29SRuslan Bukin return errcode;
7074fe6c29SRuslan Bukin
7174fe6c29SRuslan Bukin cache->msec.section = NULL;
7274fe6c29SRuslan Bukin
7374fe6c29SRuslan Bukin return pt_section_put(section);
7474fe6c29SRuslan Bukin }
7574fe6c29SRuslan Bukin
pt_msec_cache_read(struct pt_msec_cache * cache,const struct pt_mapped_section ** pmsec,struct pt_image * image,uint64_t vaddr)7674fe6c29SRuslan Bukin int pt_msec_cache_read(struct pt_msec_cache *cache,
7774fe6c29SRuslan Bukin const struct pt_mapped_section **pmsec,
7874fe6c29SRuslan Bukin struct pt_image *image, uint64_t vaddr)
7974fe6c29SRuslan Bukin {
8074fe6c29SRuslan Bukin struct pt_mapped_section *msec;
8174fe6c29SRuslan Bukin int isid, errcode;
8274fe6c29SRuslan Bukin
8374fe6c29SRuslan Bukin if (!cache || !pmsec)
8474fe6c29SRuslan Bukin return -pte_internal;
8574fe6c29SRuslan Bukin
8674fe6c29SRuslan Bukin msec = &cache->msec;
8774fe6c29SRuslan Bukin isid = cache->isid;
8874fe6c29SRuslan Bukin
8974fe6c29SRuslan Bukin errcode = pt_image_validate(image, msec, vaddr, isid);
9074fe6c29SRuslan Bukin if (errcode < 0)
9174fe6c29SRuslan Bukin return errcode;
9274fe6c29SRuslan Bukin
9374fe6c29SRuslan Bukin *pmsec = msec;
9474fe6c29SRuslan Bukin
9574fe6c29SRuslan Bukin return isid;
9674fe6c29SRuslan Bukin
9774fe6c29SRuslan Bukin }
9874fe6c29SRuslan Bukin
pt_msec_cache_fill(struct pt_msec_cache * cache,const struct pt_mapped_section ** pmsec,struct pt_image * image,const struct pt_asid * asid,uint64_t vaddr)9974fe6c29SRuslan Bukin int pt_msec_cache_fill(struct pt_msec_cache *cache,
10074fe6c29SRuslan Bukin const struct pt_mapped_section **pmsec,
10174fe6c29SRuslan Bukin struct pt_image *image, const struct pt_asid *asid,
10274fe6c29SRuslan Bukin uint64_t vaddr)
10374fe6c29SRuslan Bukin {
10474fe6c29SRuslan Bukin struct pt_mapped_section *msec;
10574fe6c29SRuslan Bukin struct pt_section *section;
10674fe6c29SRuslan Bukin int errcode, isid;
10774fe6c29SRuslan Bukin
10874fe6c29SRuslan Bukin if (!cache || !pmsec)
10974fe6c29SRuslan Bukin return -pte_internal;
11074fe6c29SRuslan Bukin
11174fe6c29SRuslan Bukin errcode = pt_msec_cache_invalidate(cache);
11274fe6c29SRuslan Bukin if (errcode < 0)
11374fe6c29SRuslan Bukin return errcode;
11474fe6c29SRuslan Bukin
11574fe6c29SRuslan Bukin msec = &cache->msec;
11674fe6c29SRuslan Bukin
11774fe6c29SRuslan Bukin isid = pt_image_find(image, msec, asid, vaddr);
11874fe6c29SRuslan Bukin if (isid < 0)
11974fe6c29SRuslan Bukin return isid;
12074fe6c29SRuslan Bukin
12174fe6c29SRuslan Bukin section = pt_msec_section(msec);
12274fe6c29SRuslan Bukin
12374fe6c29SRuslan Bukin errcode = pt_section_map(section);
12474fe6c29SRuslan Bukin if (errcode < 0) {
12574fe6c29SRuslan Bukin (void) pt_section_put(section);
12674fe6c29SRuslan Bukin msec->section = NULL;
12774fe6c29SRuslan Bukin
12874fe6c29SRuslan Bukin return errcode;
12974fe6c29SRuslan Bukin }
13074fe6c29SRuslan Bukin
13174fe6c29SRuslan Bukin *pmsec = msec;
13274fe6c29SRuslan Bukin
13374fe6c29SRuslan Bukin cache->isid = isid;
13474fe6c29SRuslan Bukin
13574fe6c29SRuslan Bukin return isid;
13674fe6c29SRuslan Bukin }
137