i915_gem.c 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_vgpu.h"
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. #include <linux/shmem_fs.h>
  35. #include <linux/slab.h>
  36. #include <linux/swap.h>
  37. #include <linux/pci.h>
  38. #include <linux/dma-buf.h>
  39. #define RQ_BUG_ON(expr)
  40. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  41. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  42. static void
  43. i915_gem_object_retire__write(struct drm_i915_gem_object *obj);
  44. static void
  45. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring);
  46. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  47. struct drm_i915_gem_object *obj);
  48. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  49. struct drm_i915_fence_reg *fence,
  50. bool enable);
  51. static bool cpu_cache_is_coherent(struct drm_device *dev,
  52. enum i915_cache_level level)
  53. {
  54. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  55. }
  56. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  57. {
  58. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  59. return true;
  60. return obj->pin_display;
  61. }
  62. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  63. {
  64. if (obj->tiling_mode)
  65. i915_gem_release_mmap(obj);
  66. /* As we do not have an associated fence register, we will force
  67. * a tiling change if we ever need to acquire one.
  68. */
  69. obj->fence_dirty = false;
  70. obj->fence_reg = I915_FENCE_REG_NONE;
  71. }
  72. /* some bookkeeping */
  73. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  74. size_t size)
  75. {
  76. spin_lock(&dev_priv->mm.object_stat_lock);
  77. dev_priv->mm.object_count++;
  78. dev_priv->mm.object_memory += size;
  79. spin_unlock(&dev_priv->mm.object_stat_lock);
  80. }
  81. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  82. size_t size)
  83. {
  84. spin_lock(&dev_priv->mm.object_stat_lock);
  85. dev_priv->mm.object_count--;
  86. dev_priv->mm.object_memory -= size;
  87. spin_unlock(&dev_priv->mm.object_stat_lock);
  88. }
  89. static int
  90. i915_gem_wait_for_error(struct i915_gpu_error *error)
  91. {
  92. int ret;
  93. #define EXIT_COND (!i915_reset_in_progress(error) || \
  94. i915_terminally_wedged(error))
  95. if (EXIT_COND)
  96. return 0;
  97. /*
  98. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  99. * userspace. If it takes that long something really bad is going on and
  100. * we should simply try to bail out and fail as gracefully as possible.
  101. */
  102. ret = wait_event_interruptible_timeout(error->reset_queue,
  103. EXIT_COND,
  104. 10*HZ);
  105. if (ret == 0) {
  106. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  107. return -EIO;
  108. } else if (ret < 0) {
  109. return ret;
  110. }
  111. #undef EXIT_COND
  112. return 0;
  113. }
  114. int i915_mutex_lock_interruptible(struct drm_device *dev)
  115. {
  116. struct drm_i915_private *dev_priv = dev->dev_private;
  117. int ret;
  118. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  119. if (ret)
  120. return ret;
  121. ret = mutex_lock_interruptible(&dev->struct_mutex);
  122. if (ret)
  123. return ret;
  124. WARN_ON(i915_verify_lists(dev));
  125. return 0;
  126. }
  127. int
  128. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  129. struct drm_file *file)
  130. {
  131. struct drm_i915_private *dev_priv = dev->dev_private;
  132. struct drm_i915_gem_get_aperture *args = data;
  133. struct drm_i915_gem_object *obj;
  134. size_t pinned;
  135. pinned = 0;
  136. mutex_lock(&dev->struct_mutex);
  137. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  138. if (i915_gem_obj_is_pinned(obj))
  139. pinned += i915_gem_obj_ggtt_size(obj);
  140. mutex_unlock(&dev->struct_mutex);
  141. args->aper_size = dev_priv->gtt.base.total;
  142. args->aper_available_size = args->aper_size - pinned;
  143. return 0;
  144. }
  145. static int
  146. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  147. {
  148. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  149. char *vaddr = obj->phys_handle->vaddr;
  150. struct sg_table *st;
  151. struct scatterlist *sg;
  152. int i;
  153. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  154. return -EINVAL;
  155. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  156. struct page *page;
  157. char *src;
  158. page = shmem_read_mapping_page(mapping, i);
  159. if (IS_ERR(page))
  160. return PTR_ERR(page);
  161. src = kmap_atomic(page);
  162. memcpy(vaddr, src, PAGE_SIZE);
  163. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  164. kunmap_atomic(src);
  165. page_cache_release(page);
  166. vaddr += PAGE_SIZE;
  167. }
  168. i915_gem_chipset_flush(obj->base.dev);
  169. st = kmalloc(sizeof(*st), GFP_KERNEL);
  170. if (st == NULL)
  171. return -ENOMEM;
  172. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  173. kfree(st);
  174. return -ENOMEM;
  175. }
  176. sg = st->sgl;
  177. sg->offset = 0;
  178. sg->length = obj->base.size;
  179. sg_dma_address(sg) = obj->phys_handle->busaddr;
  180. sg_dma_len(sg) = obj->base.size;
  181. obj->pages = st;
  182. obj->has_dma_mapping = true;
  183. return 0;
  184. }
  185. static void
  186. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
  187. {
  188. int ret;
  189. BUG_ON(obj->madv == __I915_MADV_PURGED);
  190. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  191. if (ret) {
  192. /* In the event of a disaster, abandon all caches and
  193. * hope for the best.
  194. */
  195. WARN_ON(ret != -EIO);
  196. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  197. }
  198. if (obj->madv == I915_MADV_DONTNEED)
  199. obj->dirty = 0;
  200. if (obj->dirty) {
  201. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  202. char *vaddr = obj->phys_handle->vaddr;
  203. int i;
  204. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  205. struct page *page;
  206. char *dst;
  207. page = shmem_read_mapping_page(mapping, i);
  208. if (IS_ERR(page))
  209. continue;
  210. dst = kmap_atomic(page);
  211. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  212. memcpy(dst, vaddr, PAGE_SIZE);
  213. kunmap_atomic(dst);
  214. set_page_dirty(page);
  215. if (obj->madv == I915_MADV_WILLNEED)
  216. mark_page_accessed(page);
  217. page_cache_release(page);
  218. vaddr += PAGE_SIZE;
  219. }
  220. obj->dirty = 0;
  221. }
  222. sg_free_table(obj->pages);
  223. kfree(obj->pages);
  224. obj->has_dma_mapping = false;
  225. }
  226. static void
  227. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  228. {
  229. drm_pci_free(obj->base.dev, obj->phys_handle);
  230. }
  231. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  232. .get_pages = i915_gem_object_get_pages_phys,
  233. .put_pages = i915_gem_object_put_pages_phys,
  234. .release = i915_gem_object_release_phys,
  235. };
  236. static int
  237. drop_pages(struct drm_i915_gem_object *obj)
  238. {
  239. struct i915_vma *vma, *next;
  240. int ret;
  241. drm_gem_object_reference(&obj->base);
  242. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link)
  243. if (i915_vma_unbind(vma))
  244. break;
  245. ret = i915_gem_object_put_pages(obj);
  246. drm_gem_object_unreference(&obj->base);
  247. return ret;
  248. }
  249. int
  250. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  251. int align)
  252. {
  253. drm_dma_handle_t *phys;
  254. int ret;
  255. if (obj->phys_handle) {
  256. if ((unsigned long)obj->phys_handle->vaddr & (align -1))
  257. return -EBUSY;
  258. return 0;
  259. }
  260. if (obj->madv != I915_MADV_WILLNEED)
  261. return -EFAULT;
  262. if (obj->base.filp == NULL)
  263. return -EINVAL;
  264. ret = drop_pages(obj);
  265. if (ret)
  266. return ret;
  267. /* create a new object */
  268. phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
  269. if (!phys)
  270. return -ENOMEM;
  271. obj->phys_handle = phys;
  272. obj->ops = &i915_gem_phys_ops;
  273. return i915_gem_object_get_pages(obj);
  274. }
  275. static int
  276. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  277. struct drm_i915_gem_pwrite *args,
  278. struct drm_file *file_priv)
  279. {
  280. struct drm_device *dev = obj->base.dev;
  281. void *vaddr = obj->phys_handle->vaddr + args->offset;
  282. char __user *user_data = to_user_ptr(args->data_ptr);
  283. int ret = 0;
  284. /* We manually control the domain here and pretend that it
  285. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  286. */
  287. ret = i915_gem_object_wait_rendering(obj, false);
  288. if (ret)
  289. return ret;
  290. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  291. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  292. unsigned long unwritten;
  293. /* The physical object once assigned is fixed for the lifetime
  294. * of the obj, so we can safely drop the lock and continue
  295. * to access vaddr.
  296. */
  297. mutex_unlock(&dev->struct_mutex);
  298. unwritten = copy_from_user(vaddr, user_data, args->size);
  299. mutex_lock(&dev->struct_mutex);
  300. if (unwritten) {
  301. ret = -EFAULT;
  302. goto out;
  303. }
  304. }
  305. drm_clflush_virt_range(vaddr, args->size);
  306. i915_gem_chipset_flush(dev);
  307. out:
  308. intel_fb_obj_flush(obj, false);
  309. return ret;
  310. }
  311. void *i915_gem_object_alloc(struct drm_device *dev)
  312. {
  313. struct drm_i915_private *dev_priv = dev->dev_private;
  314. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  315. }
  316. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  317. {
  318. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  319. kmem_cache_free(dev_priv->objects, obj);
  320. }
  321. static int
  322. i915_gem_create(struct drm_file *file,
  323. struct drm_device *dev,
  324. uint64_t size,
  325. uint32_t *handle_p)
  326. {
  327. struct drm_i915_gem_object *obj;
  328. int ret;
  329. u32 handle;
  330. size = roundup(size, PAGE_SIZE);
  331. if (size == 0)
  332. return -EINVAL;
  333. /* Allocate the new object */
  334. obj = i915_gem_alloc_object(dev, size);
  335. if (obj == NULL)
  336. return -ENOMEM;
  337. ret = drm_gem_handle_create(file, &obj->base, &handle);
  338. /* drop reference from allocate - handle holds it now */
  339. drm_gem_object_unreference_unlocked(&obj->base);
  340. if (ret)
  341. return ret;
  342. *handle_p = handle;
  343. return 0;
  344. }
  345. int
  346. i915_gem_dumb_create(struct drm_file *file,
  347. struct drm_device *dev,
  348. struct drm_mode_create_dumb *args)
  349. {
  350. /* have to work out size/pitch and return them */
  351. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  352. args->size = args->pitch * args->height;
  353. return i915_gem_create(file, dev,
  354. args->size, &args->handle);
  355. }
  356. /**
  357. * Creates a new mm object and returns a handle to it.
  358. */
  359. int
  360. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  361. struct drm_file *file)
  362. {
  363. struct drm_i915_gem_create *args = data;
  364. return i915_gem_create(file, dev,
  365. args->size, &args->handle);
  366. }
  367. static inline int
  368. __copy_to_user_swizzled(char __user *cpu_vaddr,
  369. const char *gpu_vaddr, int gpu_offset,
  370. int length)
  371. {
  372. int ret, cpu_offset = 0;
  373. while (length > 0) {
  374. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  375. int this_length = min(cacheline_end - gpu_offset, length);
  376. int swizzled_gpu_offset = gpu_offset ^ 64;
  377. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  378. gpu_vaddr + swizzled_gpu_offset,
  379. this_length);
  380. if (ret)
  381. return ret + length;
  382. cpu_offset += this_length;
  383. gpu_offset += this_length;
  384. length -= this_length;
  385. }
  386. return 0;
  387. }
  388. static inline int
  389. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  390. const char __user *cpu_vaddr,
  391. int length)
  392. {
  393. int ret, cpu_offset = 0;
  394. while (length > 0) {
  395. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  396. int this_length = min(cacheline_end - gpu_offset, length);
  397. int swizzled_gpu_offset = gpu_offset ^ 64;
  398. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  399. cpu_vaddr + cpu_offset,
  400. this_length);
  401. if (ret)
  402. return ret + length;
  403. cpu_offset += this_length;
  404. gpu_offset += this_length;
  405. length -= this_length;
  406. }
  407. return 0;
  408. }
  409. /*
  410. * Pins the specified object's pages and synchronizes the object with
  411. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  412. * flush the object from the CPU cache.
  413. */
  414. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  415. int *needs_clflush)
  416. {
  417. int ret;
  418. *needs_clflush = 0;
  419. if (!obj->base.filp)
  420. return -EINVAL;
  421. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  422. /* If we're not in the cpu read domain, set ourself into the gtt
  423. * read domain and manually flush cachelines (if required). This
  424. * optimizes for the case when the gpu will dirty the data
  425. * anyway again before the next pread happens. */
  426. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  427. obj->cache_level);
  428. ret = i915_gem_object_wait_rendering(obj, true);
  429. if (ret)
  430. return ret;
  431. }
  432. ret = i915_gem_object_get_pages(obj);
  433. if (ret)
  434. return ret;
  435. i915_gem_object_pin_pages(obj);
  436. return ret;
  437. }
  438. /* Per-page copy function for the shmem pread fastpath.
  439. * Flushes invalid cachelines before reading the target if
  440. * needs_clflush is set. */
  441. static int
  442. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  443. char __user *user_data,
  444. bool page_do_bit17_swizzling, bool needs_clflush)
  445. {
  446. char *vaddr;
  447. int ret;
  448. if (unlikely(page_do_bit17_swizzling))
  449. return -EINVAL;
  450. vaddr = kmap_atomic(page);
  451. if (needs_clflush)
  452. drm_clflush_virt_range(vaddr + shmem_page_offset,
  453. page_length);
  454. ret = __copy_to_user_inatomic(user_data,
  455. vaddr + shmem_page_offset,
  456. page_length);
  457. kunmap_atomic(vaddr);
  458. return ret ? -EFAULT : 0;
  459. }
  460. static void
  461. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  462. bool swizzled)
  463. {
  464. if (unlikely(swizzled)) {
  465. unsigned long start = (unsigned long) addr;
  466. unsigned long end = (unsigned long) addr + length;
  467. /* For swizzling simply ensure that we always flush both
  468. * channels. Lame, but simple and it works. Swizzled
  469. * pwrite/pread is far from a hotpath - current userspace
  470. * doesn't use it at all. */
  471. start = round_down(start, 128);
  472. end = round_up(end, 128);
  473. drm_clflush_virt_range((void *)start, end - start);
  474. } else {
  475. drm_clflush_virt_range(addr, length);
  476. }
  477. }
  478. /* Only difference to the fast-path function is that this can handle bit17
  479. * and uses non-atomic copy and kmap functions. */
  480. static int
  481. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  482. char __user *user_data,
  483. bool page_do_bit17_swizzling, bool needs_clflush)
  484. {
  485. char *vaddr;
  486. int ret;
  487. vaddr = kmap(page);
  488. if (needs_clflush)
  489. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  490. page_length,
  491. page_do_bit17_swizzling);
  492. if (page_do_bit17_swizzling)
  493. ret = __copy_to_user_swizzled(user_data,
  494. vaddr, shmem_page_offset,
  495. page_length);
  496. else
  497. ret = __copy_to_user(user_data,
  498. vaddr + shmem_page_offset,
  499. page_length);
  500. kunmap(page);
  501. return ret ? - EFAULT : 0;
  502. }
  503. static int
  504. i915_gem_shmem_pread(struct drm_device *dev,
  505. struct drm_i915_gem_object *obj,
  506. struct drm_i915_gem_pread *args,
  507. struct drm_file *file)
  508. {
  509. char __user *user_data;
  510. ssize_t remain;
  511. loff_t offset;
  512. int shmem_page_offset, page_length, ret = 0;
  513. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  514. int prefaulted = 0;
  515. int needs_clflush = 0;
  516. struct sg_page_iter sg_iter;
  517. user_data = to_user_ptr(args->data_ptr);
  518. remain = args->size;
  519. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  520. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  521. if (ret)
  522. return ret;
  523. offset = args->offset;
  524. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  525. offset >> PAGE_SHIFT) {
  526. struct page *page = sg_page_iter_page(&sg_iter);
  527. if (remain <= 0)
  528. break;
  529. /* Operation in this page
  530. *
  531. * shmem_page_offset = offset within page in shmem file
  532. * page_length = bytes to copy for this page
  533. */
  534. shmem_page_offset = offset_in_page(offset);
  535. page_length = remain;
  536. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  537. page_length = PAGE_SIZE - shmem_page_offset;
  538. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  539. (page_to_phys(page) & (1 << 17)) != 0;
  540. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  541. user_data, page_do_bit17_swizzling,
  542. needs_clflush);
  543. if (ret == 0)
  544. goto next_page;
  545. mutex_unlock(&dev->struct_mutex);
  546. if (likely(!i915.prefault_disable) && !prefaulted) {
  547. ret = fault_in_multipages_writeable(user_data, remain);
  548. /* Userspace is tricking us, but we've already clobbered
  549. * its pages with the prefault and promised to write the
  550. * data up to the first fault. Hence ignore any errors
  551. * and just continue. */
  552. (void)ret;
  553. prefaulted = 1;
  554. }
  555. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  556. user_data, page_do_bit17_swizzling,
  557. needs_clflush);
  558. mutex_lock(&dev->struct_mutex);
  559. if (ret)
  560. goto out;
  561. next_page:
  562. remain -= page_length;
  563. user_data += page_length;
  564. offset += page_length;
  565. }
  566. out:
  567. i915_gem_object_unpin_pages(obj);
  568. return ret;
  569. }
  570. /**
  571. * Reads data from the object referenced by handle.
  572. *
  573. * On error, the contents of *data are undefined.
  574. */
  575. int
  576. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  577. struct drm_file *file)
  578. {
  579. struct drm_i915_gem_pread *args = data;
  580. struct drm_i915_gem_object *obj;
  581. int ret = 0;
  582. if (args->size == 0)
  583. return 0;
  584. if (!access_ok(VERIFY_WRITE,
  585. to_user_ptr(args->data_ptr),
  586. args->size))
  587. return -EFAULT;
  588. ret = i915_mutex_lock_interruptible(dev);
  589. if (ret)
  590. return ret;
  591. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  592. if (&obj->base == NULL) {
  593. ret = -ENOENT;
  594. goto unlock;
  595. }
  596. /* Bounds check source. */
  597. if (args->offset > obj->base.size ||
  598. args->size > obj->base.size - args->offset) {
  599. ret = -EINVAL;
  600. goto out;
  601. }
  602. /* prime objects have no backing filp to GEM pread/pwrite
  603. * pages from.
  604. */
  605. if (!obj->base.filp) {
  606. ret = -EINVAL;
  607. goto out;
  608. }
  609. trace_i915_gem_object_pread(obj, args->offset, args->size);
  610. ret = i915_gem_shmem_pread(dev, obj, args, file);
  611. out:
  612. drm_gem_object_unreference(&obj->base);
  613. unlock:
  614. mutex_unlock(&dev->struct_mutex);
  615. return ret;
  616. }
  617. /* This is the fast write path which cannot handle
  618. * page faults in the source data
  619. */
  620. static inline int
  621. fast_user_write(struct io_mapping *mapping,
  622. loff_t page_base, int page_offset,
  623. char __user *user_data,
  624. int length)
  625. {
  626. void __iomem *vaddr_atomic;
  627. void *vaddr;
  628. unsigned long unwritten;
  629. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  630. /* We can use the cpu mem copy function because this is X86. */
  631. vaddr = (void __force*)vaddr_atomic + page_offset;
  632. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  633. user_data, length);
  634. io_mapping_unmap_atomic(vaddr_atomic);
  635. return unwritten;
  636. }
  637. /**
  638. * This is the fast pwrite path, where we copy the data directly from the
  639. * user into the GTT, uncached.
  640. */
  641. static int
  642. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  643. struct drm_i915_gem_object *obj,
  644. struct drm_i915_gem_pwrite *args,
  645. struct drm_file *file)
  646. {
  647. struct drm_i915_private *dev_priv = dev->dev_private;
  648. ssize_t remain;
  649. loff_t offset, page_base;
  650. char __user *user_data;
  651. int page_offset, page_length, ret;
  652. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
  653. if (ret)
  654. goto out;
  655. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  656. if (ret)
  657. goto out_unpin;
  658. ret = i915_gem_object_put_fence(obj);
  659. if (ret)
  660. goto out_unpin;
  661. user_data = to_user_ptr(args->data_ptr);
  662. remain = args->size;
  663. offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  664. intel_fb_obj_invalidate(obj, ORIGIN_GTT);
  665. while (remain > 0) {
  666. /* Operation in this page
  667. *
  668. * page_base = page offset within aperture
  669. * page_offset = offset within page
  670. * page_length = bytes to copy for this page
  671. */
  672. page_base = offset & PAGE_MASK;
  673. page_offset = offset_in_page(offset);
  674. page_length = remain;
  675. if ((page_offset + remain) > PAGE_SIZE)
  676. page_length = PAGE_SIZE - page_offset;
  677. /* If we get a fault while copying data, then (presumably) our
  678. * source page isn't available. Return the error and we'll
  679. * retry in the slow path.
  680. */
  681. if (fast_user_write(dev_priv->gtt.mappable, page_base,
  682. page_offset, user_data, page_length)) {
  683. ret = -EFAULT;
  684. goto out_flush;
  685. }
  686. remain -= page_length;
  687. user_data += page_length;
  688. offset += page_length;
  689. }
  690. out_flush:
  691. intel_fb_obj_flush(obj, false);
  692. out_unpin:
  693. i915_gem_object_ggtt_unpin(obj);
  694. out:
  695. return ret;
  696. }
  697. /* Per-page copy function for the shmem pwrite fastpath.
  698. * Flushes invalid cachelines before writing to the target if
  699. * needs_clflush_before is set and flushes out any written cachelines after
  700. * writing if needs_clflush is set. */
  701. static int
  702. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  703. char __user *user_data,
  704. bool page_do_bit17_swizzling,
  705. bool needs_clflush_before,
  706. bool needs_clflush_after)
  707. {
  708. char *vaddr;
  709. int ret;
  710. if (unlikely(page_do_bit17_swizzling))
  711. return -EINVAL;
  712. vaddr = kmap_atomic(page);
  713. if (needs_clflush_before)
  714. drm_clflush_virt_range(vaddr + shmem_page_offset,
  715. page_length);
  716. ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
  717. user_data, page_length);
  718. if (needs_clflush_after)
  719. drm_clflush_virt_range(vaddr + shmem_page_offset,
  720. page_length);
  721. kunmap_atomic(vaddr);
  722. return ret ? -EFAULT : 0;
  723. }
  724. /* Only difference to the fast-path function is that this can handle bit17
  725. * and uses non-atomic copy and kmap functions. */
  726. static int
  727. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  728. char __user *user_data,
  729. bool page_do_bit17_swizzling,
  730. bool needs_clflush_before,
  731. bool needs_clflush_after)
  732. {
  733. char *vaddr;
  734. int ret;
  735. vaddr = kmap(page);
  736. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  737. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  738. page_length,
  739. page_do_bit17_swizzling);
  740. if (page_do_bit17_swizzling)
  741. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  742. user_data,
  743. page_length);
  744. else
  745. ret = __copy_from_user(vaddr + shmem_page_offset,
  746. user_data,
  747. page_length);
  748. if (needs_clflush_after)
  749. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  750. page_length,
  751. page_do_bit17_swizzling);
  752. kunmap(page);
  753. return ret ? -EFAULT : 0;
  754. }
  755. static int
  756. i915_gem_shmem_pwrite(struct drm_device *dev,
  757. struct drm_i915_gem_object *obj,
  758. struct drm_i915_gem_pwrite *args,
  759. struct drm_file *file)
  760. {
  761. ssize_t remain;
  762. loff_t offset;
  763. char __user *user_data;
  764. int shmem_page_offset, page_length, ret = 0;
  765. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  766. int hit_slowpath = 0;
  767. int needs_clflush_after = 0;
  768. int needs_clflush_before = 0;
  769. struct sg_page_iter sg_iter;
  770. user_data = to_user_ptr(args->data_ptr);
  771. remain = args->size;
  772. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  773. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  774. /* If we're not in the cpu write domain, set ourself into the gtt
  775. * write domain and manually flush cachelines (if required). This
  776. * optimizes for the case when the gpu will use the data
  777. * right away and we therefore have to clflush anyway. */
  778. needs_clflush_after = cpu_write_needs_clflush(obj);
  779. ret = i915_gem_object_wait_rendering(obj, false);
  780. if (ret)
  781. return ret;
  782. }
  783. /* Same trick applies to invalidate partially written cachelines read
  784. * before writing. */
  785. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  786. needs_clflush_before =
  787. !cpu_cache_is_coherent(dev, obj->cache_level);
  788. ret = i915_gem_object_get_pages(obj);
  789. if (ret)
  790. return ret;
  791. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  792. i915_gem_object_pin_pages(obj);
  793. offset = args->offset;
  794. obj->dirty = 1;
  795. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  796. offset >> PAGE_SHIFT) {
  797. struct page *page = sg_page_iter_page(&sg_iter);
  798. int partial_cacheline_write;
  799. if (remain <= 0)
  800. break;
  801. /* Operation in this page
  802. *
  803. * shmem_page_offset = offset within page in shmem file
  804. * page_length = bytes to copy for this page
  805. */
  806. shmem_page_offset = offset_in_page(offset);
  807. page_length = remain;
  808. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  809. page_length = PAGE_SIZE - shmem_page_offset;
  810. /* If we don't overwrite a cacheline completely we need to be
  811. * careful to have up-to-date data by first clflushing. Don't
  812. * overcomplicate things and flush the entire patch. */
  813. partial_cacheline_write = needs_clflush_before &&
  814. ((shmem_page_offset | page_length)
  815. & (boot_cpu_data.x86_clflush_size - 1));
  816. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  817. (page_to_phys(page) & (1 << 17)) != 0;
  818. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  819. user_data, page_do_bit17_swizzling,
  820. partial_cacheline_write,
  821. needs_clflush_after);
  822. if (ret == 0)
  823. goto next_page;
  824. hit_slowpath = 1;
  825. mutex_unlock(&dev->struct_mutex);
  826. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  827. user_data, page_do_bit17_swizzling,
  828. partial_cacheline_write,
  829. needs_clflush_after);
  830. mutex_lock(&dev->struct_mutex);
  831. if (ret)
  832. goto out;
  833. next_page:
  834. remain -= page_length;
  835. user_data += page_length;
  836. offset += page_length;
  837. }
  838. out:
  839. i915_gem_object_unpin_pages(obj);
  840. if (hit_slowpath) {
  841. /*
  842. * Fixup: Flush cpu caches in case we didn't flush the dirty
  843. * cachelines in-line while writing and the object moved
  844. * out of the cpu write domain while we've dropped the lock.
  845. */
  846. if (!needs_clflush_after &&
  847. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  848. if (i915_gem_clflush_object(obj, obj->pin_display))
  849. i915_gem_chipset_flush(dev);
  850. }
  851. }
  852. if (needs_clflush_after)
  853. i915_gem_chipset_flush(dev);
  854. intel_fb_obj_flush(obj, false);
  855. return ret;
  856. }
  857. /**
  858. * Writes data to the object referenced by handle.
  859. *
  860. * On error, the contents of the buffer that were to be modified are undefined.
  861. */
  862. int
  863. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  864. struct drm_file *file)
  865. {
  866. struct drm_i915_private *dev_priv = dev->dev_private;
  867. struct drm_i915_gem_pwrite *args = data;
  868. struct drm_i915_gem_object *obj;
  869. int ret;
  870. if (args->size == 0)
  871. return 0;
  872. if (!access_ok(VERIFY_READ,
  873. to_user_ptr(args->data_ptr),
  874. args->size))
  875. return -EFAULT;
  876. if (likely(!i915.prefault_disable)) {
  877. ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
  878. args->size);
  879. if (ret)
  880. return -EFAULT;
  881. }
  882. intel_runtime_pm_get(dev_priv);
  883. ret = i915_mutex_lock_interruptible(dev);
  884. if (ret)
  885. goto put_rpm;
  886. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  887. if (&obj->base == NULL) {
  888. ret = -ENOENT;
  889. goto unlock;
  890. }
  891. /* Bounds check destination. */
  892. if (args->offset > obj->base.size ||
  893. args->size > obj->base.size - args->offset) {
  894. ret = -EINVAL;
  895. goto out;
  896. }
  897. /* prime objects have no backing filp to GEM pread/pwrite
  898. * pages from.
  899. */
  900. if (!obj->base.filp) {
  901. ret = -EINVAL;
  902. goto out;
  903. }
  904. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  905. ret = -EFAULT;
  906. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  907. * it would end up going through the fenced access, and we'll get
  908. * different detiling behavior between reading and writing.
  909. * pread/pwrite currently are reading and writing from the CPU
  910. * perspective, requiring manual detiling by the client.
  911. */
  912. if (obj->tiling_mode == I915_TILING_NONE &&
  913. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  914. cpu_write_needs_clflush(obj)) {
  915. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  916. /* Note that the gtt paths might fail with non-page-backed user
  917. * pointers (e.g. gtt mappings when moving data between
  918. * textures). Fallback to the shmem path in that case. */
  919. }
  920. if (ret == -EFAULT || ret == -ENOSPC) {
  921. if (obj->phys_handle)
  922. ret = i915_gem_phys_pwrite(obj, args, file);
  923. else
  924. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  925. }
  926. out:
  927. drm_gem_object_unreference(&obj->base);
  928. unlock:
  929. mutex_unlock(&dev->struct_mutex);
  930. put_rpm:
  931. intel_runtime_pm_put(dev_priv);
  932. return ret;
  933. }
  934. int
  935. i915_gem_check_wedge(struct i915_gpu_error *error,
  936. bool interruptible)
  937. {
  938. if (i915_reset_in_progress(error)) {
  939. /* Non-interruptible callers can't handle -EAGAIN, hence return
  940. * -EIO unconditionally for these. */
  941. if (!interruptible)
  942. return -EIO;
  943. /* Recovery complete, but the reset failed ... */
  944. if (i915_terminally_wedged(error))
  945. return -EIO;
  946. /*
  947. * Check if GPU Reset is in progress - we need intel_ring_begin
  948. * to work properly to reinit the hw state while the gpu is
  949. * still marked as reset-in-progress. Handle this with a flag.
  950. */
  951. if (!error->reload_in_reset)
  952. return -EAGAIN;
  953. }
  954. return 0;
  955. }
  956. /*
  957. * Compare arbitrary request against outstanding lazy request. Emit on match.
  958. */
  959. int
  960. i915_gem_check_olr(struct drm_i915_gem_request *req)
  961. {
  962. WARN_ON(!mutex_is_locked(&req->ring->dev->struct_mutex));
  963. if (req == req->ring->outstanding_lazy_request)
  964. i915_add_request(req);
  965. return 0;
  966. }
  967. static void fake_irq(unsigned long data)
  968. {
  969. wake_up_process((struct task_struct *)data);
  970. }
  971. static bool missed_irq(struct drm_i915_private *dev_priv,
  972. struct intel_engine_cs *ring)
  973. {
  974. return test_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings);
  975. }
  976. static int __i915_spin_request(struct drm_i915_gem_request *req)
  977. {
  978. unsigned long timeout;
  979. if (i915_gem_request_get_ring(req)->irq_refcount)
  980. return -EBUSY;
  981. timeout = jiffies + 1;
  982. while (!need_resched()) {
  983. if (i915_gem_request_completed(req, true))
  984. return 0;
  985. if (time_after_eq(jiffies, timeout))
  986. break;
  987. cpu_relax_lowlatency();
  988. }
  989. if (i915_gem_request_completed(req, false))
  990. return 0;
  991. return -EAGAIN;
  992. }
  993. /**
  994. * __i915_wait_request - wait until execution of request has finished
  995. * @req: duh!
  996. * @reset_counter: reset sequence associated with the given request
  997. * @interruptible: do an interruptible wait (normally yes)
  998. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  999. *
  1000. * Note: It is of utmost importance that the passed in seqno and reset_counter
  1001. * values have been read by the caller in an smp safe manner. Where read-side
  1002. * locks are involved, it is sufficient to read the reset_counter before
  1003. * unlocking the lock that protects the seqno. For lockless tricks, the
  1004. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  1005. * inserted.
  1006. *
  1007. * Returns 0 if the request was found within the alloted time. Else returns the
  1008. * errno with remaining time filled in timeout argument.
  1009. */
  1010. int __i915_wait_request(struct drm_i915_gem_request *req,
  1011. unsigned reset_counter,
  1012. bool interruptible,
  1013. s64 *timeout,
  1014. struct intel_rps_client *rps)
  1015. {
  1016. struct intel_engine_cs *ring = i915_gem_request_get_ring(req);
  1017. struct drm_device *dev = ring->dev;
  1018. struct drm_i915_private *dev_priv = dev->dev_private;
  1019. const bool irq_test_in_progress =
  1020. ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_ring_flag(ring);
  1021. DEFINE_WAIT(wait);
  1022. unsigned long timeout_expire;
  1023. s64 before, now;
  1024. int ret;
  1025. WARN(!intel_irqs_enabled(dev_priv), "IRQs disabled");
  1026. if (list_empty(&req->list))
  1027. return 0;
  1028. if (i915_gem_request_completed(req, true))
  1029. return 0;
  1030. timeout_expire = timeout ?
  1031. jiffies + nsecs_to_jiffies_timeout((u64)*timeout) : 0;
  1032. if (INTEL_INFO(dev_priv)->gen >= 6)
  1033. gen6_rps_boost(dev_priv, rps, req->emitted_jiffies);
  1034. /* Record current time in case interrupted by signal, or wedged */
  1035. trace_i915_gem_request_wait_begin(req);
  1036. before = ktime_get_raw_ns();
  1037. /* Optimistic spin for the next jiffie before touching IRQs */
  1038. ret = __i915_spin_request(req);
  1039. if (ret == 0)
  1040. goto out;
  1041. if (!irq_test_in_progress && WARN_ON(!ring->irq_get(ring))) {
  1042. ret = -ENODEV;
  1043. goto out;
  1044. }
  1045. for (;;) {
  1046. struct timer_list timer;
  1047. prepare_to_wait(&ring->irq_queue, &wait,
  1048. interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  1049. /* We need to check whether any gpu reset happened in between
  1050. * the caller grabbing the seqno and now ... */
  1051. if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) {
  1052. /* ... but upgrade the -EAGAIN to an -EIO if the gpu
  1053. * is truely gone. */
  1054. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1055. if (ret == 0)
  1056. ret = -EAGAIN;
  1057. break;
  1058. }
  1059. if (i915_gem_request_completed(req, false)) {
  1060. ret = 0;
  1061. break;
  1062. }
  1063. if (interruptible && signal_pending(current)) {
  1064. ret = -ERESTARTSYS;
  1065. break;
  1066. }
  1067. if (timeout && time_after_eq(jiffies, timeout_expire)) {
  1068. ret = -ETIME;
  1069. break;
  1070. }
  1071. timer.function = NULL;
  1072. if (timeout || missed_irq(dev_priv, ring)) {
  1073. unsigned long expire;
  1074. setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
  1075. expire = missed_irq(dev_priv, ring) ? jiffies + 1 : timeout_expire;
  1076. mod_timer(&timer, expire);
  1077. }
  1078. io_schedule();
  1079. if (timer.function) {
  1080. del_singleshot_timer_sync(&timer);
  1081. destroy_timer_on_stack(&timer);
  1082. }
  1083. }
  1084. if (!irq_test_in_progress)
  1085. ring->irq_put(ring);
  1086. finish_wait(&ring->irq_queue, &wait);
  1087. out:
  1088. now = ktime_get_raw_ns();
  1089. trace_i915_gem_request_wait_end(req);
  1090. if (timeout) {
  1091. s64 tres = *timeout - (now - before);
  1092. *timeout = tres < 0 ? 0 : tres;
  1093. /*
  1094. * Apparently ktime isn't accurate enough and occasionally has a
  1095. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  1096. * things up to make the test happy. We allow up to 1 jiffy.
  1097. *
  1098. * This is a regrssion from the timespec->ktime conversion.
  1099. */
  1100. if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
  1101. *timeout = 0;
  1102. }
  1103. return ret;
  1104. }
  1105. static inline void
  1106. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1107. {
  1108. struct drm_i915_file_private *file_priv = request->file_priv;
  1109. if (!file_priv)
  1110. return;
  1111. spin_lock(&file_priv->mm.lock);
  1112. list_del(&request->client_list);
  1113. request->file_priv = NULL;
  1114. spin_unlock(&file_priv->mm.lock);
  1115. }
  1116. static void i915_gem_request_retire(struct drm_i915_gem_request *request)
  1117. {
  1118. trace_i915_gem_request_retire(request);
  1119. /* We know the GPU must have read the request to have
  1120. * sent us the seqno + interrupt, so use the position
  1121. * of tail of the request to update the last known position
  1122. * of the GPU head.
  1123. *
  1124. * Note this requires that we are always called in request
  1125. * completion order.
  1126. */
  1127. request->ringbuf->last_retired_head = request->postfix;
  1128. list_del_init(&request->list);
  1129. i915_gem_request_remove_from_client(request);
  1130. put_pid(request->pid);
  1131. i915_gem_request_unreference(request);
  1132. }
  1133. static void
  1134. __i915_gem_request_retire__upto(struct drm_i915_gem_request *req)
  1135. {
  1136. struct intel_engine_cs *engine = req->ring;
  1137. struct drm_i915_gem_request *tmp;
  1138. lockdep_assert_held(&engine->dev->struct_mutex);
  1139. if (list_empty(&req->list))
  1140. return;
  1141. do {
  1142. tmp = list_first_entry(&engine->request_list,
  1143. typeof(*tmp), list);
  1144. i915_gem_request_retire(tmp);
  1145. } while (tmp != req);
  1146. WARN_ON(i915_verify_lists(engine->dev));
  1147. }
  1148. /**
  1149. * Waits for a request to be signaled, and cleans up the
  1150. * request and object lists appropriately for that event.
  1151. */
  1152. int
  1153. i915_wait_request(struct drm_i915_gem_request *req)
  1154. {
  1155. struct drm_device *dev;
  1156. struct drm_i915_private *dev_priv;
  1157. bool interruptible;
  1158. int ret;
  1159. BUG_ON(req == NULL);
  1160. dev = req->ring->dev;
  1161. dev_priv = dev->dev_private;
  1162. interruptible = dev_priv->mm.interruptible;
  1163. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1164. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1165. if (ret)
  1166. return ret;
  1167. ret = i915_gem_check_olr(req);
  1168. if (ret)
  1169. return ret;
  1170. ret = __i915_wait_request(req,
  1171. atomic_read(&dev_priv->gpu_error.reset_counter),
  1172. interruptible, NULL, NULL);
  1173. if (ret)
  1174. return ret;
  1175. __i915_gem_request_retire__upto(req);
  1176. return 0;
  1177. }
  1178. /**
  1179. * Ensures that all rendering to the object has completed and the object is
  1180. * safe to unbind from the GTT or access from the CPU.
  1181. */
  1182. int
  1183. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  1184. bool readonly)
  1185. {
  1186. int ret, i;
  1187. if (!obj->active)
  1188. return 0;
  1189. if (readonly) {
  1190. if (obj->last_write_req != NULL) {
  1191. ret = i915_wait_request(obj->last_write_req);
  1192. if (ret)
  1193. return ret;
  1194. i = obj->last_write_req->ring->id;
  1195. if (obj->last_read_req[i] == obj->last_write_req)
  1196. i915_gem_object_retire__read(obj, i);
  1197. else
  1198. i915_gem_object_retire__write(obj);
  1199. }
  1200. } else {
  1201. for (i = 0; i < I915_NUM_RINGS; i++) {
  1202. if (obj->last_read_req[i] == NULL)
  1203. continue;
  1204. ret = i915_wait_request(obj->last_read_req[i]);
  1205. if (ret)
  1206. return ret;
  1207. i915_gem_object_retire__read(obj, i);
  1208. }
  1209. RQ_BUG_ON(obj->active);
  1210. }
  1211. return 0;
  1212. }
  1213. static void
  1214. i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
  1215. struct drm_i915_gem_request *req)
  1216. {
  1217. int ring = req->ring->id;
  1218. if (obj->last_read_req[ring] == req)
  1219. i915_gem_object_retire__read(obj, ring);
  1220. else if (obj->last_write_req == req)
  1221. i915_gem_object_retire__write(obj);
  1222. __i915_gem_request_retire__upto(req);
  1223. }
  1224. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  1225. * as the object state may change during this call.
  1226. */
  1227. static __must_check int
  1228. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  1229. struct intel_rps_client *rps,
  1230. bool readonly)
  1231. {
  1232. struct drm_device *dev = obj->base.dev;
  1233. struct drm_i915_private *dev_priv = dev->dev_private;
  1234. struct drm_i915_gem_request *requests[I915_NUM_RINGS];
  1235. unsigned reset_counter;
  1236. int ret, i, n = 0;
  1237. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1238. BUG_ON(!dev_priv->mm.interruptible);
  1239. if (!obj->active)
  1240. return 0;
  1241. ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
  1242. if (ret)
  1243. return ret;
  1244. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  1245. if (readonly) {
  1246. struct drm_i915_gem_request *req;
  1247. req = obj->last_write_req;
  1248. if (req == NULL)
  1249. return 0;
  1250. ret = i915_gem_check_olr(req);
  1251. if (ret)
  1252. goto err;
  1253. requests[n++] = i915_gem_request_reference(req);
  1254. } else {
  1255. for (i = 0; i < I915_NUM_RINGS; i++) {
  1256. struct drm_i915_gem_request *req;
  1257. req = obj->last_read_req[i];
  1258. if (req == NULL)
  1259. continue;
  1260. ret = i915_gem_check_olr(req);
  1261. if (ret)
  1262. goto err;
  1263. requests[n++] = i915_gem_request_reference(req);
  1264. }
  1265. }
  1266. mutex_unlock(&dev->struct_mutex);
  1267. for (i = 0; ret == 0 && i < n; i++)
  1268. ret = __i915_wait_request(requests[i], reset_counter, true,
  1269. NULL, rps);
  1270. mutex_lock(&dev->struct_mutex);
  1271. err:
  1272. for (i = 0; i < n; i++) {
  1273. if (ret == 0)
  1274. i915_gem_object_retire_request(obj, requests[i]);
  1275. i915_gem_request_unreference(requests[i]);
  1276. }
  1277. return ret;
  1278. }
  1279. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  1280. {
  1281. struct drm_i915_file_private *fpriv = file->driver_priv;
  1282. return &fpriv->rps;
  1283. }
  1284. /**
  1285. * Called when user space prepares to use an object with the CPU, either
  1286. * through the mmap ioctl's mapping or a GTT mapping.
  1287. */
  1288. int
  1289. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1290. struct drm_file *file)
  1291. {
  1292. struct drm_i915_gem_set_domain *args = data;
  1293. struct drm_i915_gem_object *obj;
  1294. uint32_t read_domains = args->read_domains;
  1295. uint32_t write_domain = args->write_domain;
  1296. int ret;
  1297. /* Only handle setting domains to types used by the CPU. */
  1298. if (write_domain & I915_GEM_GPU_DOMAINS)
  1299. return -EINVAL;
  1300. if (read_domains & I915_GEM_GPU_DOMAINS)
  1301. return -EINVAL;
  1302. /* Having something in the write domain implies it's in the read
  1303. * domain, and only that read domain. Enforce that in the request.
  1304. */
  1305. if (write_domain != 0 && read_domains != write_domain)
  1306. return -EINVAL;
  1307. ret = i915_mutex_lock_interruptible(dev);
  1308. if (ret)
  1309. return ret;
  1310. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1311. if (&obj->base == NULL) {
  1312. ret = -ENOENT;
  1313. goto unlock;
  1314. }
  1315. /* Try to flush the object off the GPU without holding the lock.
  1316. * We will repeat the flush holding the lock in the normal manner
  1317. * to catch cases where we are gazumped.
  1318. */
  1319. ret = i915_gem_object_wait_rendering__nonblocking(obj,
  1320. to_rps_client(file),
  1321. !write_domain);
  1322. if (ret)
  1323. goto unref;
  1324. if (read_domains & I915_GEM_DOMAIN_GTT)
  1325. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1326. else
  1327. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1328. unref:
  1329. drm_gem_object_unreference(&obj->base);
  1330. unlock:
  1331. mutex_unlock(&dev->struct_mutex);
  1332. return ret;
  1333. }
  1334. /**
  1335. * Called when user space has done writes to this buffer
  1336. */
  1337. int
  1338. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1339. struct drm_file *file)
  1340. {
  1341. struct drm_i915_gem_sw_finish *args = data;
  1342. struct drm_i915_gem_object *obj;
  1343. int ret = 0;
  1344. ret = i915_mutex_lock_interruptible(dev);
  1345. if (ret)
  1346. return ret;
  1347. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1348. if (&obj->base == NULL) {
  1349. ret = -ENOENT;
  1350. goto unlock;
  1351. }
  1352. /* Pinned buffers may be scanout, so flush the cache */
  1353. if (obj->pin_display)
  1354. i915_gem_object_flush_cpu_write_domain(obj);
  1355. drm_gem_object_unreference(&obj->base);
  1356. unlock:
  1357. mutex_unlock(&dev->struct_mutex);
  1358. return ret;
  1359. }
  1360. /**
  1361. * Maps the contents of an object, returning the address it is mapped
  1362. * into.
  1363. *
  1364. * While the mapping holds a reference on the contents of the object, it doesn't
  1365. * imply a ref on the object itself.
  1366. *
  1367. * IMPORTANT:
  1368. *
  1369. * DRM driver writers who look a this function as an example for how to do GEM
  1370. * mmap support, please don't implement mmap support like here. The modern way
  1371. * to implement DRM mmap support is with an mmap offset ioctl (like
  1372. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1373. * That way debug tooling like valgrind will understand what's going on, hiding
  1374. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1375. * does cpu mmaps this way because we didn't know better.
  1376. */
  1377. int
  1378. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1379. struct drm_file *file)
  1380. {
  1381. struct drm_i915_gem_mmap *args = data;
  1382. struct drm_gem_object *obj;
  1383. unsigned long addr;
  1384. if (args->flags & ~(I915_MMAP_WC))
  1385. return -EINVAL;
  1386. if (args->flags & I915_MMAP_WC && !cpu_has_pat)
  1387. return -ENODEV;
  1388. obj = drm_gem_object_lookup(dev, file, args->handle);
  1389. if (obj == NULL)
  1390. return -ENOENT;
  1391. /* prime objects have no backing filp to GEM mmap
  1392. * pages from.
  1393. */
  1394. if (!obj->filp) {
  1395. drm_gem_object_unreference_unlocked(obj);
  1396. return -EINVAL;
  1397. }
  1398. addr = vm_mmap(obj->filp, 0, args->size,
  1399. PROT_READ | PROT_WRITE, MAP_SHARED,
  1400. args->offset);
  1401. if (args->flags & I915_MMAP_WC) {
  1402. struct mm_struct *mm = current->mm;
  1403. struct vm_area_struct *vma;
  1404. down_write(&mm->mmap_sem);
  1405. vma = find_vma(mm, addr);
  1406. if (vma)
  1407. vma->vm_page_prot =
  1408. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1409. else
  1410. addr = -ENOMEM;
  1411. up_write(&mm->mmap_sem);
  1412. }
  1413. drm_gem_object_unreference_unlocked(obj);
  1414. if (IS_ERR((void *)addr))
  1415. return addr;
  1416. args->addr_ptr = (uint64_t) addr;
  1417. return 0;
  1418. }
  1419. /**
  1420. * i915_gem_fault - fault a page into the GTT
  1421. * vma: VMA in question
  1422. * vmf: fault info
  1423. *
  1424. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1425. * from userspace. The fault handler takes care of binding the object to
  1426. * the GTT (if needed), allocating and programming a fence register (again,
  1427. * only if needed based on whether the old reg is still valid or the object
  1428. * is tiled) and inserting a new PTE into the faulting process.
  1429. *
  1430. * Note that the faulting process may involve evicting existing objects
  1431. * from the GTT and/or fence registers to make room. So performance may
  1432. * suffer if the GTT working set is large or there are few fence registers
  1433. * left.
  1434. */
  1435. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1436. {
  1437. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1438. struct drm_device *dev = obj->base.dev;
  1439. struct drm_i915_private *dev_priv = dev->dev_private;
  1440. struct i915_ggtt_view view = i915_ggtt_view_normal;
  1441. pgoff_t page_offset;
  1442. unsigned long pfn;
  1443. int ret = 0;
  1444. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1445. intel_runtime_pm_get(dev_priv);
  1446. /* We don't use vmf->pgoff since that has the fake offset */
  1447. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1448. PAGE_SHIFT;
  1449. ret = i915_mutex_lock_interruptible(dev);
  1450. if (ret)
  1451. goto out;
  1452. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1453. /* Try to flush the object off the GPU first without holding the lock.
  1454. * Upon reacquiring the lock, we will perform our sanity checks and then
  1455. * repeat the flush holding the lock in the normal manner to catch cases
  1456. * where we are gazumped.
  1457. */
  1458. ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
  1459. if (ret)
  1460. goto unlock;
  1461. /* Access to snoopable pages through the GTT is incoherent. */
  1462. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1463. ret = -EFAULT;
  1464. goto unlock;
  1465. }
  1466. /* Use a partial view if the object is bigger than the aperture. */
  1467. if (obj->base.size >= dev_priv->gtt.mappable_end &&
  1468. obj->tiling_mode == I915_TILING_NONE) {
  1469. static const unsigned int chunk_size = 256; // 1 MiB
  1470. memset(&view, 0, sizeof(view));
  1471. view.type = I915_GGTT_VIEW_PARTIAL;
  1472. view.params.partial.offset = rounddown(page_offset, chunk_size);
  1473. view.params.partial.size =
  1474. min_t(unsigned int,
  1475. chunk_size,
  1476. (vma->vm_end - vma->vm_start)/PAGE_SIZE -
  1477. view.params.partial.offset);
  1478. }
  1479. /* Now pin it into the GTT if needed */
  1480. ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
  1481. if (ret)
  1482. goto unlock;
  1483. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1484. if (ret)
  1485. goto unpin;
  1486. ret = i915_gem_object_get_fence(obj);
  1487. if (ret)
  1488. goto unpin;
  1489. /* Finally, remap it using the new GTT offset */
  1490. pfn = dev_priv->gtt.mappable_base +
  1491. i915_gem_obj_ggtt_offset_view(obj, &view);
  1492. pfn >>= PAGE_SHIFT;
  1493. if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
  1494. /* Overriding existing pages in partial view does not cause
  1495. * us any trouble as TLBs are still valid because the fault
  1496. * is due to userspace losing part of the mapping or never
  1497. * having accessed it before (at this partials' range).
  1498. */
  1499. unsigned long base = vma->vm_start +
  1500. (view.params.partial.offset << PAGE_SHIFT);
  1501. unsigned int i;
  1502. for (i = 0; i < view.params.partial.size; i++) {
  1503. ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
  1504. if (ret)
  1505. break;
  1506. }
  1507. obj->fault_mappable = true;
  1508. } else {
  1509. if (!obj->fault_mappable) {
  1510. unsigned long size = min_t(unsigned long,
  1511. vma->vm_end - vma->vm_start,
  1512. obj->base.size);
  1513. int i;
  1514. for (i = 0; i < size >> PAGE_SHIFT; i++) {
  1515. ret = vm_insert_pfn(vma,
  1516. (unsigned long)vma->vm_start + i * PAGE_SIZE,
  1517. pfn + i);
  1518. if (ret)
  1519. break;
  1520. }
  1521. obj->fault_mappable = true;
  1522. } else
  1523. ret = vm_insert_pfn(vma,
  1524. (unsigned long)vmf->virtual_address,
  1525. pfn + page_offset);
  1526. }
  1527. unpin:
  1528. i915_gem_object_ggtt_unpin_view(obj, &view);
  1529. unlock:
  1530. mutex_unlock(&dev->struct_mutex);
  1531. out:
  1532. switch (ret) {
  1533. case -EIO:
  1534. /*
  1535. * We eat errors when the gpu is terminally wedged to avoid
  1536. * userspace unduly crashing (gl has no provisions for mmaps to
  1537. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1538. * and so needs to be reported.
  1539. */
  1540. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1541. ret = VM_FAULT_SIGBUS;
  1542. break;
  1543. }
  1544. case -EAGAIN:
  1545. /*
  1546. * EAGAIN means the gpu is hung and we'll wait for the error
  1547. * handler to reset everything when re-faulting in
  1548. * i915_mutex_lock_interruptible.
  1549. */
  1550. case 0:
  1551. case -ERESTARTSYS:
  1552. case -EINTR:
  1553. case -EBUSY:
  1554. /*
  1555. * EBUSY is ok: this just means that another thread
  1556. * already did the job.
  1557. */
  1558. ret = VM_FAULT_NOPAGE;
  1559. break;
  1560. case -ENOMEM:
  1561. ret = VM_FAULT_OOM;
  1562. break;
  1563. case -ENOSPC:
  1564. case -EFAULT:
  1565. ret = VM_FAULT_SIGBUS;
  1566. break;
  1567. default:
  1568. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1569. ret = VM_FAULT_SIGBUS;
  1570. break;
  1571. }
  1572. intel_runtime_pm_put(dev_priv);
  1573. return ret;
  1574. }
  1575. /**
  1576. * i915_gem_release_mmap - remove physical page mappings
  1577. * @obj: obj in question
  1578. *
  1579. * Preserve the reservation of the mmapping with the DRM core code, but
  1580. * relinquish ownership of the pages back to the system.
  1581. *
  1582. * It is vital that we remove the page mapping if we have mapped a tiled
  1583. * object through the GTT and then lose the fence register due to
  1584. * resource pressure. Similarly if the object has been moved out of the
  1585. * aperture, than pages mapped into userspace must be revoked. Removing the
  1586. * mapping will then trigger a page fault on the next user access, allowing
  1587. * fixup by i915_gem_fault().
  1588. */
  1589. void
  1590. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1591. {
  1592. if (!obj->fault_mappable)
  1593. return;
  1594. drm_vma_node_unmap(&obj->base.vma_node,
  1595. obj->base.dev->anon_inode->i_mapping);
  1596. obj->fault_mappable = false;
  1597. }
  1598. void
  1599. i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
  1600. {
  1601. struct drm_i915_gem_object *obj;
  1602. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  1603. i915_gem_release_mmap(obj);
  1604. }
  1605. uint32_t
  1606. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1607. {
  1608. uint32_t gtt_size;
  1609. if (INTEL_INFO(dev)->gen >= 4 ||
  1610. tiling_mode == I915_TILING_NONE)
  1611. return size;
  1612. /* Previous chips need a power-of-two fence region when tiling */
  1613. if (INTEL_INFO(dev)->gen == 3)
  1614. gtt_size = 1024*1024;
  1615. else
  1616. gtt_size = 512*1024;
  1617. while (gtt_size < size)
  1618. gtt_size <<= 1;
  1619. return gtt_size;
  1620. }
  1621. /**
  1622. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1623. * @obj: object to check
  1624. *
  1625. * Return the required GTT alignment for an object, taking into account
  1626. * potential fence register mapping.
  1627. */
  1628. uint32_t
  1629. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1630. int tiling_mode, bool fenced)
  1631. {
  1632. /*
  1633. * Minimum alignment is 4k (GTT page size), but might be greater
  1634. * if a fence register is needed for the object.
  1635. */
  1636. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1637. tiling_mode == I915_TILING_NONE)
  1638. return 4096;
  1639. /*
  1640. * Previous chips need to be aligned to the size of the smallest
  1641. * fence register that can contain the object.
  1642. */
  1643. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1644. }
  1645. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1646. {
  1647. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1648. int ret;
  1649. if (drm_vma_node_has_offset(&obj->base.vma_node))
  1650. return 0;
  1651. dev_priv->mm.shrinker_no_lock_stealing = true;
  1652. ret = drm_gem_create_mmap_offset(&obj->base);
  1653. if (ret != -ENOSPC)
  1654. goto out;
  1655. /* Badly fragmented mmap space? The only way we can recover
  1656. * space is by destroying unwanted objects. We can't randomly release
  1657. * mmap_offsets as userspace expects them to be persistent for the
  1658. * lifetime of the objects. The closest we can is to release the
  1659. * offsets on purgeable objects by truncating it and marking it purged,
  1660. * which prevents userspace from ever using that object again.
  1661. */
  1662. i915_gem_shrink(dev_priv,
  1663. obj->base.size >> PAGE_SHIFT,
  1664. I915_SHRINK_BOUND |
  1665. I915_SHRINK_UNBOUND |
  1666. I915_SHRINK_PURGEABLE);
  1667. ret = drm_gem_create_mmap_offset(&obj->base);
  1668. if (ret != -ENOSPC)
  1669. goto out;
  1670. i915_gem_shrink_all(dev_priv);
  1671. ret = drm_gem_create_mmap_offset(&obj->base);
  1672. out:
  1673. dev_priv->mm.shrinker_no_lock_stealing = false;
  1674. return ret;
  1675. }
  1676. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1677. {
  1678. drm_gem_free_mmap_offset(&obj->base);
  1679. }
  1680. int
  1681. i915_gem_mmap_gtt(struct drm_file *file,
  1682. struct drm_device *dev,
  1683. uint32_t handle,
  1684. uint64_t *offset)
  1685. {
  1686. struct drm_i915_gem_object *obj;
  1687. int ret;
  1688. ret = i915_mutex_lock_interruptible(dev);
  1689. if (ret)
  1690. return ret;
  1691. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1692. if (&obj->base == NULL) {
  1693. ret = -ENOENT;
  1694. goto unlock;
  1695. }
  1696. if (obj->madv != I915_MADV_WILLNEED) {
  1697. DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
  1698. ret = -EFAULT;
  1699. goto out;
  1700. }
  1701. ret = i915_gem_object_create_mmap_offset(obj);
  1702. if (ret)
  1703. goto out;
  1704. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1705. out:
  1706. drm_gem_object_unreference(&obj->base);
  1707. unlock:
  1708. mutex_unlock(&dev->struct_mutex);
  1709. return ret;
  1710. }
  1711. /**
  1712. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1713. * @dev: DRM device
  1714. * @data: GTT mapping ioctl data
  1715. * @file: GEM object info
  1716. *
  1717. * Simply returns the fake offset to userspace so it can mmap it.
  1718. * The mmap call will end up in drm_gem_mmap(), which will set things
  1719. * up so we can get faults in the handler above.
  1720. *
  1721. * The fault handler will take care of binding the object into the GTT
  1722. * (since it may have been evicted to make room for something), allocating
  1723. * a fence register, and mapping the appropriate aperture address into
  1724. * userspace.
  1725. */
  1726. int
  1727. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1728. struct drm_file *file)
  1729. {
  1730. struct drm_i915_gem_mmap_gtt *args = data;
  1731. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1732. }
  1733. /* Immediately discard the backing storage */
  1734. static void
  1735. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1736. {
  1737. i915_gem_object_free_mmap_offset(obj);
  1738. if (obj->base.filp == NULL)
  1739. return;
  1740. /* Our goal here is to return as much of the memory as
  1741. * is possible back to the system as we are called from OOM.
  1742. * To do this we must instruct the shmfs to drop all of its
  1743. * backing pages, *now*.
  1744. */
  1745. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1746. obj->madv = __I915_MADV_PURGED;
  1747. }
  1748. /* Try to discard unwanted pages */
  1749. static void
  1750. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1751. {
  1752. struct address_space *mapping;
  1753. switch (obj->madv) {
  1754. case I915_MADV_DONTNEED:
  1755. i915_gem_object_truncate(obj);
  1756. case __I915_MADV_PURGED:
  1757. return;
  1758. }
  1759. if (obj->base.filp == NULL)
  1760. return;
  1761. mapping = file_inode(obj->base.filp)->i_mapping,
  1762. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1763. }
  1764. static void
  1765. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1766. {
  1767. struct sg_page_iter sg_iter;
  1768. int ret;
  1769. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1770. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1771. if (ret) {
  1772. /* In the event of a disaster, abandon all caches and
  1773. * hope for the best.
  1774. */
  1775. WARN_ON(ret != -EIO);
  1776. i915_gem_clflush_object(obj, true);
  1777. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1778. }
  1779. if (i915_gem_object_needs_bit17_swizzle(obj))
  1780. i915_gem_object_save_bit_17_swizzle(obj);
  1781. if (obj->madv == I915_MADV_DONTNEED)
  1782. obj->dirty = 0;
  1783. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1784. struct page *page = sg_page_iter_page(&sg_iter);
  1785. if (obj->dirty)
  1786. set_page_dirty(page);
  1787. if (obj->madv == I915_MADV_WILLNEED)
  1788. mark_page_accessed(page);
  1789. page_cache_release(page);
  1790. }
  1791. obj->dirty = 0;
  1792. sg_free_table(obj->pages);
  1793. kfree(obj->pages);
  1794. }
  1795. int
  1796. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1797. {
  1798. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1799. if (obj->pages == NULL)
  1800. return 0;
  1801. if (obj->pages_pin_count)
  1802. return -EBUSY;
  1803. BUG_ON(i915_gem_obj_bound_any(obj));
  1804. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1805. * array, hence protect them from being reaped by removing them from gtt
  1806. * lists early. */
  1807. list_del(&obj->global_list);
  1808. ops->put_pages(obj);
  1809. obj->pages = NULL;
  1810. i915_gem_object_invalidate(obj);
  1811. return 0;
  1812. }
  1813. static int
  1814. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1815. {
  1816. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1817. int page_count, i;
  1818. struct address_space *mapping;
  1819. struct sg_table *st;
  1820. struct scatterlist *sg;
  1821. struct sg_page_iter sg_iter;
  1822. struct page *page;
  1823. unsigned long last_pfn = 0; /* suppress gcc warning */
  1824. gfp_t gfp;
  1825. /* Assert that the object is not currently in any GPU domain. As it
  1826. * wasn't in the GTT, there shouldn't be any way it could have been in
  1827. * a GPU cache
  1828. */
  1829. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1830. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1831. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1832. if (st == NULL)
  1833. return -ENOMEM;
  1834. page_count = obj->base.size / PAGE_SIZE;
  1835. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1836. kfree(st);
  1837. return -ENOMEM;
  1838. }
  1839. /* Get the list of pages out of our struct file. They'll be pinned
  1840. * at this point until we release them.
  1841. *
  1842. * Fail silently without starting the shrinker
  1843. */
  1844. mapping = file_inode(obj->base.filp)->i_mapping;
  1845. gfp = mapping_gfp_mask(mapping);
  1846. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1847. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1848. sg = st->sgl;
  1849. st->nents = 0;
  1850. for (i = 0; i < page_count; i++) {
  1851. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1852. if (IS_ERR(page)) {
  1853. i915_gem_shrink(dev_priv,
  1854. page_count,
  1855. I915_SHRINK_BOUND |
  1856. I915_SHRINK_UNBOUND |
  1857. I915_SHRINK_PURGEABLE);
  1858. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1859. }
  1860. if (IS_ERR(page)) {
  1861. /* We've tried hard to allocate the memory by reaping
  1862. * our own buffer, now let the real VM do its job and
  1863. * go down in flames if truly OOM.
  1864. */
  1865. i915_gem_shrink_all(dev_priv);
  1866. page = shmem_read_mapping_page(mapping, i);
  1867. if (IS_ERR(page))
  1868. goto err_pages;
  1869. }
  1870. #ifdef CONFIG_SWIOTLB
  1871. if (swiotlb_nr_tbl()) {
  1872. st->nents++;
  1873. sg_set_page(sg, page, PAGE_SIZE, 0);
  1874. sg = sg_next(sg);
  1875. continue;
  1876. }
  1877. #endif
  1878. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1879. if (i)
  1880. sg = sg_next(sg);
  1881. st->nents++;
  1882. sg_set_page(sg, page, PAGE_SIZE, 0);
  1883. } else {
  1884. sg->length += PAGE_SIZE;
  1885. }
  1886. last_pfn = page_to_pfn(page);
  1887. /* Check that the i965g/gm workaround works. */
  1888. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  1889. }
  1890. #ifdef CONFIG_SWIOTLB
  1891. if (!swiotlb_nr_tbl())
  1892. #endif
  1893. sg_mark_end(sg);
  1894. obj->pages = st;
  1895. if (i915_gem_object_needs_bit17_swizzle(obj))
  1896. i915_gem_object_do_bit_17_swizzle(obj);
  1897. if (obj->tiling_mode != I915_TILING_NONE &&
  1898. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  1899. i915_gem_object_pin_pages(obj);
  1900. return 0;
  1901. err_pages:
  1902. sg_mark_end(sg);
  1903. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1904. page_cache_release(sg_page_iter_page(&sg_iter));
  1905. sg_free_table(st);
  1906. kfree(st);
  1907. /* shmemfs first checks if there is enough memory to allocate the page
  1908. * and reports ENOSPC should there be insufficient, along with the usual
  1909. * ENOMEM for a genuine allocation failure.
  1910. *
  1911. * We use ENOSPC in our driver to mean that we have run out of aperture
  1912. * space and so want to translate the error from shmemfs back to our
  1913. * usual understanding of ENOMEM.
  1914. */
  1915. if (PTR_ERR(page) == -ENOSPC)
  1916. return -ENOMEM;
  1917. else
  1918. return PTR_ERR(page);
  1919. }
  1920. /* Ensure that the associated pages are gathered from the backing storage
  1921. * and pinned into our object. i915_gem_object_get_pages() may be called
  1922. * multiple times before they are released by a single call to
  1923. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1924. * either as a result of memory pressure (reaping pages under the shrinker)
  1925. * or as the object is itself released.
  1926. */
  1927. int
  1928. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1929. {
  1930. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1931. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1932. int ret;
  1933. if (obj->pages)
  1934. return 0;
  1935. if (obj->madv != I915_MADV_WILLNEED) {
  1936. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  1937. return -EFAULT;
  1938. }
  1939. BUG_ON(obj->pages_pin_count);
  1940. ret = ops->get_pages(obj);
  1941. if (ret)
  1942. return ret;
  1943. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1944. obj->get_page.sg = obj->pages->sgl;
  1945. obj->get_page.last = 0;
  1946. return 0;
  1947. }
  1948. void i915_vma_move_to_active(struct i915_vma *vma,
  1949. struct drm_i915_gem_request *req)
  1950. {
  1951. struct drm_i915_gem_object *obj = vma->obj;
  1952. struct intel_engine_cs *ring;
  1953. ring = i915_gem_request_get_ring(req);
  1954. /* Add a reference if we're newly entering the active list. */
  1955. if (obj->active == 0)
  1956. drm_gem_object_reference(&obj->base);
  1957. obj->active |= intel_ring_flag(ring);
  1958. list_move_tail(&obj->ring_list[ring->id], &ring->active_list);
  1959. i915_gem_request_assign(&obj->last_read_req[ring->id], req);
  1960. list_move_tail(&vma->mm_list, &vma->vm->active_list);
  1961. }
  1962. static void
  1963. i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
  1964. {
  1965. RQ_BUG_ON(obj->last_write_req == NULL);
  1966. RQ_BUG_ON(!(obj->active & intel_ring_flag(obj->last_write_req->ring)));
  1967. i915_gem_request_assign(&obj->last_write_req, NULL);
  1968. intel_fb_obj_flush(obj, true);
  1969. }
  1970. static void
  1971. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring)
  1972. {
  1973. struct i915_vma *vma;
  1974. RQ_BUG_ON(obj->last_read_req[ring] == NULL);
  1975. RQ_BUG_ON(!(obj->active & (1 << ring)));
  1976. list_del_init(&obj->ring_list[ring]);
  1977. i915_gem_request_assign(&obj->last_read_req[ring], NULL);
  1978. if (obj->last_write_req && obj->last_write_req->ring->id == ring)
  1979. i915_gem_object_retire__write(obj);
  1980. obj->active &= ~(1 << ring);
  1981. if (obj->active)
  1982. return;
  1983. list_for_each_entry(vma, &obj->vma_list, vma_link) {
  1984. if (!list_empty(&vma->mm_list))
  1985. list_move_tail(&vma->mm_list, &vma->vm->inactive_list);
  1986. }
  1987. i915_gem_request_assign(&obj->last_fenced_req, NULL);
  1988. drm_gem_object_unreference(&obj->base);
  1989. }
  1990. static int
  1991. i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
  1992. {
  1993. struct drm_i915_private *dev_priv = dev->dev_private;
  1994. struct intel_engine_cs *ring;
  1995. int ret, i, j;
  1996. /* Carefully retire all requests without writing to the rings */
  1997. for_each_ring(ring, dev_priv, i) {
  1998. ret = intel_ring_idle(ring);
  1999. if (ret)
  2000. return ret;
  2001. }
  2002. i915_gem_retire_requests(dev);
  2003. /* Finally reset hw state */
  2004. for_each_ring(ring, dev_priv, i) {
  2005. intel_ring_init_seqno(ring, seqno);
  2006. for (j = 0; j < ARRAY_SIZE(ring->semaphore.sync_seqno); j++)
  2007. ring->semaphore.sync_seqno[j] = 0;
  2008. }
  2009. return 0;
  2010. }
  2011. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  2012. {
  2013. struct drm_i915_private *dev_priv = dev->dev_private;
  2014. int ret;
  2015. if (seqno == 0)
  2016. return -EINVAL;
  2017. /* HWS page needs to be set less than what we
  2018. * will inject to ring
  2019. */
  2020. ret = i915_gem_init_seqno(dev, seqno - 1);
  2021. if (ret)
  2022. return ret;
  2023. /* Carefully set the last_seqno value so that wrap
  2024. * detection still works
  2025. */
  2026. dev_priv->next_seqno = seqno;
  2027. dev_priv->last_seqno = seqno - 1;
  2028. if (dev_priv->last_seqno == 0)
  2029. dev_priv->last_seqno--;
  2030. return 0;
  2031. }
  2032. int
  2033. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  2034. {
  2035. struct drm_i915_private *dev_priv = dev->dev_private;
  2036. /* reserve 0 for non-seqno */
  2037. if (dev_priv->next_seqno == 0) {
  2038. int ret = i915_gem_init_seqno(dev, 0);
  2039. if (ret)
  2040. return ret;
  2041. dev_priv->next_seqno = 1;
  2042. }
  2043. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  2044. return 0;
  2045. }
  2046. /*
  2047. * NB: This function is not allowed to fail. Doing so would mean the the
  2048. * request is not being tracked for completion but the work itself is
  2049. * going to happen on the hardware. This would be a Bad Thing(tm).
  2050. */
  2051. void __i915_add_request(struct drm_i915_gem_request *request,
  2052. struct drm_file *file,
  2053. struct drm_i915_gem_object *obj,
  2054. bool flush_caches)
  2055. {
  2056. struct intel_engine_cs *ring;
  2057. struct drm_i915_private *dev_priv;
  2058. struct intel_ringbuffer *ringbuf;
  2059. u32 request_start;
  2060. int ret;
  2061. if (WARN_ON(request == NULL))
  2062. return;
  2063. ring = request->ring;
  2064. dev_priv = ring->dev->dev_private;
  2065. ringbuf = request->ringbuf;
  2066. WARN_ON(request != ring->outstanding_lazy_request);
  2067. /*
  2068. * To ensure that this call will not fail, space for its emissions
  2069. * should already have been reserved in the ring buffer. Let the ring
  2070. * know that it is time to use that space up.
  2071. */
  2072. intel_ring_reserved_space_use(ringbuf);
  2073. request_start = intel_ring_get_tail(ringbuf);
  2074. /*
  2075. * Emit any outstanding flushes - execbuf can fail to emit the flush
  2076. * after having emitted the batchbuffer command. Hence we need to fix
  2077. * things up similar to emitting the lazy request. The difference here
  2078. * is that the flush _must_ happen before the next request, no matter
  2079. * what.
  2080. */
  2081. if (flush_caches) {
  2082. if (i915.enable_execlists)
  2083. ret = logical_ring_flush_all_caches(request);
  2084. else
  2085. ret = intel_ring_flush_all_caches(request);
  2086. /* Not allowed to fail! */
  2087. WARN(ret, "*_ring_flush_all_caches failed: %d!\n", ret);
  2088. }
  2089. /* Record the position of the start of the request so that
  2090. * should we detect the updated seqno part-way through the
  2091. * GPU processing the request, we never over-estimate the
  2092. * position of the head.
  2093. */
  2094. request->postfix = intel_ring_get_tail(ringbuf);
  2095. if (i915.enable_execlists)
  2096. ret = ring->emit_request(ringbuf, request);
  2097. else {
  2098. ret = ring->add_request(request);
  2099. request->tail = intel_ring_get_tail(ringbuf);
  2100. }
  2101. /* Not allowed to fail! */
  2102. WARN(ret, "emit|add_request failed: %d!\n", ret);
  2103. request->head = request_start;
  2104. /* Whilst this request exists, batch_obj will be on the
  2105. * active_list, and so will hold the active reference. Only when this
  2106. * request is retired will the the batch_obj be moved onto the
  2107. * inactive_list and lose its active reference. Hence we do not need
  2108. * to explicitly hold another reference here.
  2109. */
  2110. request->batch_obj = obj;
  2111. request->emitted_jiffies = jiffies;
  2112. list_add_tail(&request->list, &ring->request_list);
  2113. request->file_priv = NULL;
  2114. if (file) {
  2115. struct drm_i915_file_private *file_priv = file->driver_priv;
  2116. spin_lock(&file_priv->mm.lock);
  2117. request->file_priv = file_priv;
  2118. list_add_tail(&request->client_list,
  2119. &file_priv->mm.request_list);
  2120. spin_unlock(&file_priv->mm.lock);
  2121. request->pid = get_pid(task_pid(current));
  2122. }
  2123. trace_i915_gem_request_add(request);
  2124. ring->outstanding_lazy_request = NULL;
  2125. i915_queue_hangcheck(ring->dev);
  2126. queue_delayed_work(dev_priv->wq,
  2127. &dev_priv->mm.retire_work,
  2128. round_jiffies_up_relative(HZ));
  2129. intel_mark_busy(dev_priv->dev);
  2130. /* Sanity check that the reserved size was large enough. */
  2131. intel_ring_reserved_space_end(ringbuf);
  2132. }
  2133. static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
  2134. const struct intel_context *ctx)
  2135. {
  2136. unsigned long elapsed;
  2137. elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
  2138. if (ctx->hang_stats.banned)
  2139. return true;
  2140. if (ctx->hang_stats.ban_period_seconds &&
  2141. elapsed <= ctx->hang_stats.ban_period_seconds) {
  2142. if (!i915_gem_context_is_default(ctx)) {
  2143. DRM_DEBUG("context hanging too fast, banning!\n");
  2144. return true;
  2145. } else if (i915_stop_ring_allow_ban(dev_priv)) {
  2146. if (i915_stop_ring_allow_warn(dev_priv))
  2147. DRM_ERROR("gpu hanging too fast, banning!\n");
  2148. return true;
  2149. }
  2150. }
  2151. return false;
  2152. }
  2153. static void i915_set_reset_status(struct drm_i915_private *dev_priv,
  2154. struct intel_context *ctx,
  2155. const bool guilty)
  2156. {
  2157. struct i915_ctx_hang_stats *hs;
  2158. if (WARN_ON(!ctx))
  2159. return;
  2160. hs = &ctx->hang_stats;
  2161. if (guilty) {
  2162. hs->banned = i915_context_is_banned(dev_priv, ctx);
  2163. hs->batch_active++;
  2164. hs->guilty_ts = get_seconds();
  2165. } else {
  2166. hs->batch_pending++;
  2167. }
  2168. }
  2169. void i915_gem_request_free(struct kref *req_ref)
  2170. {
  2171. struct drm_i915_gem_request *req = container_of(req_ref,
  2172. typeof(*req), ref);
  2173. struct intel_context *ctx = req->ctx;
  2174. if (ctx) {
  2175. if (i915.enable_execlists) {
  2176. struct intel_engine_cs *ring = req->ring;
  2177. if (ctx != ring->default_context)
  2178. intel_lr_context_unpin(ring, ctx);
  2179. }
  2180. i915_gem_context_unreference(ctx);
  2181. }
  2182. kmem_cache_free(req->i915->requests, req);
  2183. }
  2184. int i915_gem_request_alloc(struct intel_engine_cs *ring,
  2185. struct intel_context *ctx,
  2186. struct drm_i915_gem_request **req_out)
  2187. {
  2188. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  2189. struct drm_i915_gem_request *req;
  2190. int ret;
  2191. if (!req_out)
  2192. return -EINVAL;
  2193. if ((*req_out = ring->outstanding_lazy_request) != NULL)
  2194. return 0;
  2195. req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
  2196. if (req == NULL)
  2197. return -ENOMEM;
  2198. ret = i915_gem_get_seqno(ring->dev, &req->seqno);
  2199. if (ret)
  2200. goto err;
  2201. kref_init(&req->ref);
  2202. req->i915 = dev_priv;
  2203. req->ring = ring;
  2204. req->ctx = ctx;
  2205. i915_gem_context_reference(req->ctx);
  2206. if (i915.enable_execlists)
  2207. ret = intel_logical_ring_alloc_request_extras(req);
  2208. else
  2209. ret = intel_ring_alloc_request_extras(req);
  2210. if (ret) {
  2211. i915_gem_context_unreference(req->ctx);
  2212. goto err;
  2213. }
  2214. /*
  2215. * Reserve space in the ring buffer for all the commands required to
  2216. * eventually emit this request. This is to guarantee that the
  2217. * i915_add_request() call can't fail. Note that the reserve may need
  2218. * to be redone if the request is not actually submitted straight
  2219. * away, e.g. because a GPU scheduler has deferred it.
  2220. *
  2221. * Note further that this call merely notes the reserve request. A
  2222. * subsequent call to *_ring_begin() is required to actually ensure
  2223. * that the reservation is available. Without the begin, if the
  2224. * request creator immediately submitted the request without adding
  2225. * any commands to it then there might not actually be sufficient
  2226. * room for the submission commands. Unfortunately, the current
  2227. * *_ring_begin() implementations potentially call back here to
  2228. * i915_gem_request_alloc(). Thus calling _begin() here would lead to
  2229. * infinite recursion! Until that back call path is removed, it is
  2230. * necessary to do a manual _begin() outside.
  2231. */
  2232. intel_ring_reserved_space_reserve(req->ringbuf, MIN_SPACE_FOR_ADD_REQUEST);
  2233. *req_out = ring->outstanding_lazy_request = req;
  2234. return 0;
  2235. err:
  2236. kmem_cache_free(dev_priv->requests, req);
  2237. return ret;
  2238. }
  2239. void i915_gem_request_cancel(struct drm_i915_gem_request *req)
  2240. {
  2241. intel_ring_reserved_space_cancel(req->ringbuf);
  2242. i915_gem_request_unreference(req);
  2243. }
  2244. struct drm_i915_gem_request *
  2245. i915_gem_find_active_request(struct intel_engine_cs *ring)
  2246. {
  2247. struct drm_i915_gem_request *request;
  2248. list_for_each_entry(request, &ring->request_list, list) {
  2249. if (i915_gem_request_completed(request, false))
  2250. continue;
  2251. return request;
  2252. }
  2253. return NULL;
  2254. }
  2255. static void i915_gem_reset_ring_status(struct drm_i915_private *dev_priv,
  2256. struct intel_engine_cs *ring)
  2257. {
  2258. struct drm_i915_gem_request *request;
  2259. bool ring_hung;
  2260. request = i915_gem_find_active_request(ring);
  2261. if (request == NULL)
  2262. return;
  2263. ring_hung = ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2264. i915_set_reset_status(dev_priv, request->ctx, ring_hung);
  2265. list_for_each_entry_continue(request, &ring->request_list, list)
  2266. i915_set_reset_status(dev_priv, request->ctx, false);
  2267. }
  2268. static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv,
  2269. struct intel_engine_cs *ring)
  2270. {
  2271. while (!list_empty(&ring->active_list)) {
  2272. struct drm_i915_gem_object *obj;
  2273. obj = list_first_entry(&ring->active_list,
  2274. struct drm_i915_gem_object,
  2275. ring_list[ring->id]);
  2276. i915_gem_object_retire__read(obj, ring->id);
  2277. }
  2278. /*
  2279. * Clear the execlists queue up before freeing the requests, as those
  2280. * are the ones that keep the context and ringbuffer backing objects
  2281. * pinned in place.
  2282. */
  2283. while (!list_empty(&ring->execlist_queue)) {
  2284. struct drm_i915_gem_request *submit_req;
  2285. submit_req = list_first_entry(&ring->execlist_queue,
  2286. struct drm_i915_gem_request,
  2287. execlist_link);
  2288. list_del(&submit_req->execlist_link);
  2289. if (submit_req->ctx != ring->default_context)
  2290. intel_lr_context_unpin(ring, submit_req->ctx);
  2291. i915_gem_request_unreference(submit_req);
  2292. }
  2293. /*
  2294. * We must free the requests after all the corresponding objects have
  2295. * been moved off active lists. Which is the same order as the normal
  2296. * retire_requests function does. This is important if object hold
  2297. * implicit references on things like e.g. ppgtt address spaces through
  2298. * the request.
  2299. */
  2300. while (!list_empty(&ring->request_list)) {
  2301. struct drm_i915_gem_request *request;
  2302. request = list_first_entry(&ring->request_list,
  2303. struct drm_i915_gem_request,
  2304. list);
  2305. i915_gem_request_retire(request);
  2306. }
  2307. /* This may not have been flushed before the reset, so clean it now */
  2308. i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
  2309. }
  2310. void i915_gem_restore_fences(struct drm_device *dev)
  2311. {
  2312. struct drm_i915_private *dev_priv = dev->dev_private;
  2313. int i;
  2314. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  2315. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  2316. /*
  2317. * Commit delayed tiling changes if we have an object still
  2318. * attached to the fence, otherwise just clear the fence.
  2319. */
  2320. if (reg->obj) {
  2321. i915_gem_object_update_fence(reg->obj, reg,
  2322. reg->obj->tiling_mode);
  2323. } else {
  2324. i915_gem_write_fence(dev, i, NULL);
  2325. }
  2326. }
  2327. }
  2328. void i915_gem_reset(struct drm_device *dev)
  2329. {
  2330. struct drm_i915_private *dev_priv = dev->dev_private;
  2331. struct intel_engine_cs *ring;
  2332. int i;
  2333. /*
  2334. * Before we free the objects from the requests, we need to inspect
  2335. * them for finding the guilty party. As the requests only borrow
  2336. * their reference to the objects, the inspection must be done first.
  2337. */
  2338. for_each_ring(ring, dev_priv, i)
  2339. i915_gem_reset_ring_status(dev_priv, ring);
  2340. for_each_ring(ring, dev_priv, i)
  2341. i915_gem_reset_ring_cleanup(dev_priv, ring);
  2342. i915_gem_context_reset(dev);
  2343. i915_gem_restore_fences(dev);
  2344. WARN_ON(i915_verify_lists(dev));
  2345. }
  2346. /**
  2347. * This function clears the request list as sequence numbers are passed.
  2348. */
  2349. void
  2350. i915_gem_retire_requests_ring(struct intel_engine_cs *ring)
  2351. {
  2352. WARN_ON(i915_verify_lists(ring->dev));
  2353. /* Retire requests first as we use it above for the early return.
  2354. * If we retire requests last, we may use a later seqno and so clear
  2355. * the requests lists without clearing the active list, leading to
  2356. * confusion.
  2357. */
  2358. while (!list_empty(&ring->request_list)) {
  2359. struct drm_i915_gem_request *request;
  2360. request = list_first_entry(&ring->request_list,
  2361. struct drm_i915_gem_request,
  2362. list);
  2363. if (!i915_gem_request_completed(request, true))
  2364. break;
  2365. i915_gem_request_retire(request);
  2366. }
  2367. /* Move any buffers on the active list that are no longer referenced
  2368. * by the ringbuffer to the flushing/inactive lists as appropriate,
  2369. * before we free the context associated with the requests.
  2370. */
  2371. while (!list_empty(&ring->active_list)) {
  2372. struct drm_i915_gem_object *obj;
  2373. obj = list_first_entry(&ring->active_list,
  2374. struct drm_i915_gem_object,
  2375. ring_list[ring->id]);
  2376. if (!list_empty(&obj->last_read_req[ring->id]->list))
  2377. break;
  2378. i915_gem_object_retire__read(obj, ring->id);
  2379. }
  2380. if (unlikely(ring->trace_irq_req &&
  2381. i915_gem_request_completed(ring->trace_irq_req, true))) {
  2382. ring->irq_put(ring);
  2383. i915_gem_request_assign(&ring->trace_irq_req, NULL);
  2384. }
  2385. WARN_ON(i915_verify_lists(ring->dev));
  2386. }
  2387. bool
  2388. i915_gem_retire_requests(struct drm_device *dev)
  2389. {
  2390. struct drm_i915_private *dev_priv = dev->dev_private;
  2391. struct intel_engine_cs *ring;
  2392. bool idle = true;
  2393. int i;
  2394. for_each_ring(ring, dev_priv, i) {
  2395. i915_gem_retire_requests_ring(ring);
  2396. idle &= list_empty(&ring->request_list);
  2397. if (i915.enable_execlists) {
  2398. unsigned long flags;
  2399. spin_lock_irqsave(&ring->execlist_lock, flags);
  2400. idle &= list_empty(&ring->execlist_queue);
  2401. spin_unlock_irqrestore(&ring->execlist_lock, flags);
  2402. intel_execlists_retire_requests(ring);
  2403. }
  2404. }
  2405. if (idle)
  2406. mod_delayed_work(dev_priv->wq,
  2407. &dev_priv->mm.idle_work,
  2408. msecs_to_jiffies(100));
  2409. return idle;
  2410. }
  2411. static void
  2412. i915_gem_retire_work_handler(struct work_struct *work)
  2413. {
  2414. struct drm_i915_private *dev_priv =
  2415. container_of(work, typeof(*dev_priv), mm.retire_work.work);
  2416. struct drm_device *dev = dev_priv->dev;
  2417. bool idle;
  2418. /* Come back later if the device is busy... */
  2419. idle = false;
  2420. if (mutex_trylock(&dev->struct_mutex)) {
  2421. idle = i915_gem_retire_requests(dev);
  2422. mutex_unlock(&dev->struct_mutex);
  2423. }
  2424. if (!idle)
  2425. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2426. round_jiffies_up_relative(HZ));
  2427. }
  2428. static void
  2429. i915_gem_idle_work_handler(struct work_struct *work)
  2430. {
  2431. struct drm_i915_private *dev_priv =
  2432. container_of(work, typeof(*dev_priv), mm.idle_work.work);
  2433. struct drm_device *dev = dev_priv->dev;
  2434. struct intel_engine_cs *ring;
  2435. int i;
  2436. for_each_ring(ring, dev_priv, i)
  2437. if (!list_empty(&ring->request_list))
  2438. return;
  2439. intel_mark_idle(dev);
  2440. if (mutex_trylock(&dev->struct_mutex)) {
  2441. struct intel_engine_cs *ring;
  2442. int i;
  2443. for_each_ring(ring, dev_priv, i)
  2444. i915_gem_batch_pool_fini(&ring->batch_pool);
  2445. mutex_unlock(&dev->struct_mutex);
  2446. }
  2447. }
  2448. /**
  2449. * Ensures that an object will eventually get non-busy by flushing any required
  2450. * write domains, emitting any outstanding lazy request and retiring and
  2451. * completed requests.
  2452. */
  2453. static int
  2454. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2455. {
  2456. int ret, i;
  2457. if (!obj->active)
  2458. return 0;
  2459. for (i = 0; i < I915_NUM_RINGS; i++) {
  2460. struct drm_i915_gem_request *req;
  2461. req = obj->last_read_req[i];
  2462. if (req == NULL)
  2463. continue;
  2464. if (list_empty(&req->list))
  2465. goto retire;
  2466. ret = i915_gem_check_olr(req);
  2467. if (ret)
  2468. return ret;
  2469. if (i915_gem_request_completed(req, true)) {
  2470. __i915_gem_request_retire__upto(req);
  2471. retire:
  2472. i915_gem_object_retire__read(obj, i);
  2473. }
  2474. }
  2475. return 0;
  2476. }
  2477. /**
  2478. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2479. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2480. *
  2481. * Returns 0 if successful, else an error is returned with the remaining time in
  2482. * the timeout parameter.
  2483. * -ETIME: object is still busy after timeout
  2484. * -ERESTARTSYS: signal interrupted the wait
  2485. * -ENONENT: object doesn't exist
  2486. * Also possible, but rare:
  2487. * -EAGAIN: GPU wedged
  2488. * -ENOMEM: damn
  2489. * -ENODEV: Internal IRQ fail
  2490. * -E?: The add request failed
  2491. *
  2492. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2493. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2494. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2495. * without holding struct_mutex the object may become re-busied before this
  2496. * function completes. A similar but shorter * race condition exists in the busy
  2497. * ioctl
  2498. */
  2499. int
  2500. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2501. {
  2502. struct drm_i915_private *dev_priv = dev->dev_private;
  2503. struct drm_i915_gem_wait *args = data;
  2504. struct drm_i915_gem_object *obj;
  2505. struct drm_i915_gem_request *req[I915_NUM_RINGS];
  2506. unsigned reset_counter;
  2507. int i, n = 0;
  2508. int ret;
  2509. if (args->flags != 0)
  2510. return -EINVAL;
  2511. ret = i915_mutex_lock_interruptible(dev);
  2512. if (ret)
  2513. return ret;
  2514. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2515. if (&obj->base == NULL) {
  2516. mutex_unlock(&dev->struct_mutex);
  2517. return -ENOENT;
  2518. }
  2519. /* Need to make sure the object gets inactive eventually. */
  2520. ret = i915_gem_object_flush_active(obj);
  2521. if (ret)
  2522. goto out;
  2523. if (!obj->active)
  2524. goto out;
  2525. /* Do this after OLR check to make sure we make forward progress polling
  2526. * on this IOCTL with a timeout == 0 (like busy ioctl)
  2527. */
  2528. if (args->timeout_ns == 0) {
  2529. ret = -ETIME;
  2530. goto out;
  2531. }
  2532. drm_gem_object_unreference(&obj->base);
  2533. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2534. for (i = 0; i < I915_NUM_RINGS; i++) {
  2535. if (obj->last_read_req[i] == NULL)
  2536. continue;
  2537. req[n++] = i915_gem_request_reference(obj->last_read_req[i]);
  2538. }
  2539. mutex_unlock(&dev->struct_mutex);
  2540. for (i = 0; i < n; i++) {
  2541. if (ret == 0)
  2542. ret = __i915_wait_request(req[i], reset_counter, true,
  2543. args->timeout_ns > 0 ? &args->timeout_ns : NULL,
  2544. file->driver_priv);
  2545. i915_gem_request_unreference__unlocked(req[i]);
  2546. }
  2547. return ret;
  2548. out:
  2549. drm_gem_object_unreference(&obj->base);
  2550. mutex_unlock(&dev->struct_mutex);
  2551. return ret;
  2552. }
  2553. static int
  2554. __i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2555. struct intel_engine_cs *to,
  2556. struct drm_i915_gem_request *from_req,
  2557. struct drm_i915_gem_request **to_req)
  2558. {
  2559. struct intel_engine_cs *from;
  2560. int ret;
  2561. from = i915_gem_request_get_ring(from_req);
  2562. if (to == from)
  2563. return 0;
  2564. if (i915_gem_request_completed(from_req, true))
  2565. return 0;
  2566. ret = i915_gem_check_olr(from_req);
  2567. if (ret)
  2568. return ret;
  2569. if (!i915_semaphore_is_enabled(obj->base.dev)) {
  2570. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  2571. ret = __i915_wait_request(from_req,
  2572. atomic_read(&i915->gpu_error.reset_counter),
  2573. i915->mm.interruptible,
  2574. NULL,
  2575. &i915->rps.semaphores);
  2576. if (ret)
  2577. return ret;
  2578. i915_gem_object_retire_request(obj, from_req);
  2579. } else {
  2580. int idx = intel_ring_sync_index(from, to);
  2581. u32 seqno = i915_gem_request_get_seqno(from_req);
  2582. WARN_ON(!to_req);
  2583. if (seqno <= from->semaphore.sync_seqno[idx])
  2584. return 0;
  2585. if (*to_req == NULL) {
  2586. ret = i915_gem_request_alloc(to, to->default_context, to_req);
  2587. if (ret)
  2588. return ret;
  2589. }
  2590. trace_i915_gem_ring_sync_to(from, to, from_req);
  2591. ret = to->semaphore.sync_to(to, from, seqno);
  2592. if (ret)
  2593. return ret;
  2594. /* We use last_read_req because sync_to()
  2595. * might have just caused seqno wrap under
  2596. * the radar.
  2597. */
  2598. from->semaphore.sync_seqno[idx] =
  2599. i915_gem_request_get_seqno(obj->last_read_req[from->id]);
  2600. }
  2601. return 0;
  2602. }
  2603. /**
  2604. * i915_gem_object_sync - sync an object to a ring.
  2605. *
  2606. * @obj: object which may be in use on another ring.
  2607. * @to: ring we wish to use the object on. May be NULL.
  2608. * @to_req: request we wish to use the object for. See below.
  2609. * This will be allocated and returned if a request is
  2610. * required but not passed in.
  2611. *
  2612. * This code is meant to abstract object synchronization with the GPU.
  2613. * Calling with NULL implies synchronizing the object with the CPU
  2614. * rather than a particular GPU ring. Conceptually we serialise writes
  2615. * between engines inside the GPU. We only allow one engine to write
  2616. * into a buffer at any time, but multiple readers. To ensure each has
  2617. * a coherent view of memory, we must:
  2618. *
  2619. * - If there is an outstanding write request to the object, the new
  2620. * request must wait for it to complete (either CPU or in hw, requests
  2621. * on the same ring will be naturally ordered).
  2622. *
  2623. * - If we are a write request (pending_write_domain is set), the new
  2624. * request must wait for outstanding read requests to complete.
  2625. *
  2626. * For CPU synchronisation (NULL to) no request is required. For syncing with
  2627. * rings to_req must be non-NULL. However, a request does not have to be
  2628. * pre-allocated. If *to_req is NULL and sync commands will be emitted then a
  2629. * request will be allocated automatically and returned through *to_req. Note
  2630. * that it is not guaranteed that commands will be emitted (because the system
  2631. * might already be idle). Hence there is no need to create a request that
  2632. * might never have any work submitted. Note further that if a request is
  2633. * returned in *to_req, it is the responsibility of the caller to submit
  2634. * that request (after potentially adding more work to it).
  2635. *
  2636. * Returns 0 if successful, else propagates up the lower layer error.
  2637. */
  2638. int
  2639. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2640. struct intel_engine_cs *to,
  2641. struct drm_i915_gem_request **to_req)
  2642. {
  2643. const bool readonly = obj->base.pending_write_domain == 0;
  2644. struct drm_i915_gem_request *req[I915_NUM_RINGS];
  2645. int ret, i, n;
  2646. if (!obj->active)
  2647. return 0;
  2648. if (to == NULL)
  2649. return i915_gem_object_wait_rendering(obj, readonly);
  2650. n = 0;
  2651. if (readonly) {
  2652. if (obj->last_write_req)
  2653. req[n++] = obj->last_write_req;
  2654. } else {
  2655. for (i = 0; i < I915_NUM_RINGS; i++)
  2656. if (obj->last_read_req[i])
  2657. req[n++] = obj->last_read_req[i];
  2658. }
  2659. for (i = 0; i < n; i++) {
  2660. ret = __i915_gem_object_sync(obj, to, req[i], to_req);
  2661. if (ret)
  2662. return ret;
  2663. }
  2664. return 0;
  2665. }
  2666. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2667. {
  2668. u32 old_write_domain, old_read_domains;
  2669. /* Force a pagefault for domain tracking on next user access */
  2670. i915_gem_release_mmap(obj);
  2671. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2672. return;
  2673. /* Wait for any direct GTT access to complete */
  2674. mb();
  2675. old_read_domains = obj->base.read_domains;
  2676. old_write_domain = obj->base.write_domain;
  2677. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2678. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2679. trace_i915_gem_object_change_domain(obj,
  2680. old_read_domains,
  2681. old_write_domain);
  2682. }
  2683. int i915_vma_unbind(struct i915_vma *vma)
  2684. {
  2685. struct drm_i915_gem_object *obj = vma->obj;
  2686. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2687. int ret;
  2688. if (list_empty(&vma->vma_link))
  2689. return 0;
  2690. if (!drm_mm_node_allocated(&vma->node)) {
  2691. i915_gem_vma_destroy(vma);
  2692. return 0;
  2693. }
  2694. if (vma->pin_count)
  2695. return -EBUSY;
  2696. BUG_ON(obj->pages == NULL);
  2697. ret = i915_gem_object_wait_rendering(obj, false);
  2698. if (ret)
  2699. return ret;
  2700. /* Continue on if we fail due to EIO, the GPU is hung so we
  2701. * should be safe and we need to cleanup or else we might
  2702. * cause memory corruption through use-after-free.
  2703. */
  2704. if (i915_is_ggtt(vma->vm) &&
  2705. vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  2706. i915_gem_object_finish_gtt(obj);
  2707. /* release the fence reg _after_ flushing */
  2708. ret = i915_gem_object_put_fence(obj);
  2709. if (ret)
  2710. return ret;
  2711. }
  2712. trace_i915_vma_unbind(vma);
  2713. vma->vm->unbind_vma(vma);
  2714. vma->bound = 0;
  2715. list_del_init(&vma->mm_list);
  2716. if (i915_is_ggtt(vma->vm)) {
  2717. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  2718. obj->map_and_fenceable = false;
  2719. } else if (vma->ggtt_view.pages) {
  2720. sg_free_table(vma->ggtt_view.pages);
  2721. kfree(vma->ggtt_view.pages);
  2722. vma->ggtt_view.pages = NULL;
  2723. }
  2724. }
  2725. drm_mm_remove_node(&vma->node);
  2726. i915_gem_vma_destroy(vma);
  2727. /* Since the unbound list is global, only move to that list if
  2728. * no more VMAs exist. */
  2729. if (list_empty(&obj->vma_list)) {
  2730. i915_gem_gtt_finish_object(obj);
  2731. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2732. }
  2733. /* And finally now the object is completely decoupled from this vma,
  2734. * we can drop its hold on the backing storage and allow it to be
  2735. * reaped by the shrinker.
  2736. */
  2737. i915_gem_object_unpin_pages(obj);
  2738. return 0;
  2739. }
  2740. int i915_gpu_idle(struct drm_device *dev)
  2741. {
  2742. struct drm_i915_private *dev_priv = dev->dev_private;
  2743. struct intel_engine_cs *ring;
  2744. int ret, i;
  2745. /* Flush everything onto the inactive list. */
  2746. for_each_ring(ring, dev_priv, i) {
  2747. if (!i915.enable_execlists) {
  2748. struct drm_i915_gem_request *req;
  2749. ret = i915_gem_request_alloc(ring, ring->default_context, &req);
  2750. if (ret)
  2751. return ret;
  2752. ret = i915_switch_context(req);
  2753. if (ret) {
  2754. i915_gem_request_cancel(req);
  2755. return ret;
  2756. }
  2757. i915_add_request_no_flush(req);
  2758. }
  2759. WARN_ON(ring->outstanding_lazy_request);
  2760. ret = intel_ring_idle(ring);
  2761. if (ret)
  2762. return ret;
  2763. }
  2764. WARN_ON(i915_verify_lists(dev));
  2765. return 0;
  2766. }
  2767. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2768. struct drm_i915_gem_object *obj)
  2769. {
  2770. struct drm_i915_private *dev_priv = dev->dev_private;
  2771. int fence_reg;
  2772. int fence_pitch_shift;
  2773. if (INTEL_INFO(dev)->gen >= 6) {
  2774. fence_reg = FENCE_REG_SANDYBRIDGE_0;
  2775. fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2776. } else {
  2777. fence_reg = FENCE_REG_965_0;
  2778. fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  2779. }
  2780. fence_reg += reg * 8;
  2781. /* To w/a incoherency with non-atomic 64-bit register updates,
  2782. * we split the 64-bit update into two 32-bit writes. In order
  2783. * for a partial fence not to be evaluated between writes, we
  2784. * precede the update with write to turn off the fence register,
  2785. * and only enable the fence as the last step.
  2786. *
  2787. * For extra levels of paranoia, we make sure each step lands
  2788. * before applying the next step.
  2789. */
  2790. I915_WRITE(fence_reg, 0);
  2791. POSTING_READ(fence_reg);
  2792. if (obj) {
  2793. u32 size = i915_gem_obj_ggtt_size(obj);
  2794. uint64_t val;
  2795. /* Adjust fence size to match tiled area */
  2796. if (obj->tiling_mode != I915_TILING_NONE) {
  2797. uint32_t row_size = obj->stride *
  2798. (obj->tiling_mode == I915_TILING_Y ? 32 : 8);
  2799. size = (size / row_size) * row_size;
  2800. }
  2801. val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
  2802. 0xfffff000) << 32;
  2803. val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
  2804. val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
  2805. if (obj->tiling_mode == I915_TILING_Y)
  2806. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2807. val |= I965_FENCE_REG_VALID;
  2808. I915_WRITE(fence_reg + 4, val >> 32);
  2809. POSTING_READ(fence_reg + 4);
  2810. I915_WRITE(fence_reg + 0, val);
  2811. POSTING_READ(fence_reg);
  2812. } else {
  2813. I915_WRITE(fence_reg + 4, 0);
  2814. POSTING_READ(fence_reg + 4);
  2815. }
  2816. }
  2817. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2818. struct drm_i915_gem_object *obj)
  2819. {
  2820. struct drm_i915_private *dev_priv = dev->dev_private;
  2821. u32 val;
  2822. if (obj) {
  2823. u32 size = i915_gem_obj_ggtt_size(obj);
  2824. int pitch_val;
  2825. int tile_width;
  2826. WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
  2827. (size & -size) != size ||
  2828. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2829. "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2830. i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
  2831. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2832. tile_width = 128;
  2833. else
  2834. tile_width = 512;
  2835. /* Note: pitch better be a power of two tile widths */
  2836. pitch_val = obj->stride / tile_width;
  2837. pitch_val = ffs(pitch_val) - 1;
  2838. val = i915_gem_obj_ggtt_offset(obj);
  2839. if (obj->tiling_mode == I915_TILING_Y)
  2840. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2841. val |= I915_FENCE_SIZE_BITS(size);
  2842. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2843. val |= I830_FENCE_REG_VALID;
  2844. } else
  2845. val = 0;
  2846. if (reg < 8)
  2847. reg = FENCE_REG_830_0 + reg * 4;
  2848. else
  2849. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2850. I915_WRITE(reg, val);
  2851. POSTING_READ(reg);
  2852. }
  2853. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2854. struct drm_i915_gem_object *obj)
  2855. {
  2856. struct drm_i915_private *dev_priv = dev->dev_private;
  2857. uint32_t val;
  2858. if (obj) {
  2859. u32 size = i915_gem_obj_ggtt_size(obj);
  2860. uint32_t pitch_val;
  2861. WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
  2862. (size & -size) != size ||
  2863. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2864. "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
  2865. i915_gem_obj_ggtt_offset(obj), size);
  2866. pitch_val = obj->stride / 128;
  2867. pitch_val = ffs(pitch_val) - 1;
  2868. val = i915_gem_obj_ggtt_offset(obj);
  2869. if (obj->tiling_mode == I915_TILING_Y)
  2870. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2871. val |= I830_FENCE_SIZE_BITS(size);
  2872. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2873. val |= I830_FENCE_REG_VALID;
  2874. } else
  2875. val = 0;
  2876. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2877. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2878. }
  2879. inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
  2880. {
  2881. return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
  2882. }
  2883. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2884. struct drm_i915_gem_object *obj)
  2885. {
  2886. struct drm_i915_private *dev_priv = dev->dev_private;
  2887. /* Ensure that all CPU reads are completed before installing a fence
  2888. * and all writes before removing the fence.
  2889. */
  2890. if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
  2891. mb();
  2892. WARN(obj && (!obj->stride || !obj->tiling_mode),
  2893. "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
  2894. obj->stride, obj->tiling_mode);
  2895. if (IS_GEN2(dev))
  2896. i830_write_fence_reg(dev, reg, obj);
  2897. else if (IS_GEN3(dev))
  2898. i915_write_fence_reg(dev, reg, obj);
  2899. else if (INTEL_INFO(dev)->gen >= 4)
  2900. i965_write_fence_reg(dev, reg, obj);
  2901. /* And similarly be paranoid that no direct access to this region
  2902. * is reordered to before the fence is installed.
  2903. */
  2904. if (i915_gem_object_needs_mb(obj))
  2905. mb();
  2906. }
  2907. static inline int fence_number(struct drm_i915_private *dev_priv,
  2908. struct drm_i915_fence_reg *fence)
  2909. {
  2910. return fence - dev_priv->fence_regs;
  2911. }
  2912. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2913. struct drm_i915_fence_reg *fence,
  2914. bool enable)
  2915. {
  2916. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2917. int reg = fence_number(dev_priv, fence);
  2918. i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  2919. if (enable) {
  2920. obj->fence_reg = reg;
  2921. fence->obj = obj;
  2922. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2923. } else {
  2924. obj->fence_reg = I915_FENCE_REG_NONE;
  2925. fence->obj = NULL;
  2926. list_del_init(&fence->lru_list);
  2927. }
  2928. obj->fence_dirty = false;
  2929. }
  2930. static int
  2931. i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
  2932. {
  2933. if (obj->last_fenced_req) {
  2934. int ret = i915_wait_request(obj->last_fenced_req);
  2935. if (ret)
  2936. return ret;
  2937. i915_gem_request_assign(&obj->last_fenced_req, NULL);
  2938. }
  2939. return 0;
  2940. }
  2941. int
  2942. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2943. {
  2944. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2945. struct drm_i915_fence_reg *fence;
  2946. int ret;
  2947. ret = i915_gem_object_wait_fence(obj);
  2948. if (ret)
  2949. return ret;
  2950. if (obj->fence_reg == I915_FENCE_REG_NONE)
  2951. return 0;
  2952. fence = &dev_priv->fence_regs[obj->fence_reg];
  2953. if (WARN_ON(fence->pin_count))
  2954. return -EBUSY;
  2955. i915_gem_object_fence_lost(obj);
  2956. i915_gem_object_update_fence(obj, fence, false);
  2957. return 0;
  2958. }
  2959. static struct drm_i915_fence_reg *
  2960. i915_find_fence_reg(struct drm_device *dev)
  2961. {
  2962. struct drm_i915_private *dev_priv = dev->dev_private;
  2963. struct drm_i915_fence_reg *reg, *avail;
  2964. int i;
  2965. /* First try to find a free reg */
  2966. avail = NULL;
  2967. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2968. reg = &dev_priv->fence_regs[i];
  2969. if (!reg->obj)
  2970. return reg;
  2971. if (!reg->pin_count)
  2972. avail = reg;
  2973. }
  2974. if (avail == NULL)
  2975. goto deadlock;
  2976. /* None available, try to steal one or wait for a user to finish */
  2977. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2978. if (reg->pin_count)
  2979. continue;
  2980. return reg;
  2981. }
  2982. deadlock:
  2983. /* Wait for completion of pending flips which consume fences */
  2984. if (intel_has_pending_fb_unpin(dev))
  2985. return ERR_PTR(-EAGAIN);
  2986. return ERR_PTR(-EDEADLK);
  2987. }
  2988. /**
  2989. * i915_gem_object_get_fence - set up fencing for an object
  2990. * @obj: object to map through a fence reg
  2991. *
  2992. * When mapping objects through the GTT, userspace wants to be able to write
  2993. * to them without having to worry about swizzling if the object is tiled.
  2994. * This function walks the fence regs looking for a free one for @obj,
  2995. * stealing one if it can't find any.
  2996. *
  2997. * It then sets up the reg based on the object's properties: address, pitch
  2998. * and tiling format.
  2999. *
  3000. * For an untiled surface, this removes any existing fence.
  3001. */
  3002. int
  3003. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  3004. {
  3005. struct drm_device *dev = obj->base.dev;
  3006. struct drm_i915_private *dev_priv = dev->dev_private;
  3007. bool enable = obj->tiling_mode != I915_TILING_NONE;
  3008. struct drm_i915_fence_reg *reg;
  3009. int ret;
  3010. /* Have we updated the tiling parameters upon the object and so
  3011. * will need to serialise the write to the associated fence register?
  3012. */
  3013. if (obj->fence_dirty) {
  3014. ret = i915_gem_object_wait_fence(obj);
  3015. if (ret)
  3016. return ret;
  3017. }
  3018. /* Just update our place in the LRU if our fence is getting reused. */
  3019. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  3020. reg = &dev_priv->fence_regs[obj->fence_reg];
  3021. if (!obj->fence_dirty) {
  3022. list_move_tail(&reg->lru_list,
  3023. &dev_priv->mm.fence_list);
  3024. return 0;
  3025. }
  3026. } else if (enable) {
  3027. if (WARN_ON(!obj->map_and_fenceable))
  3028. return -EINVAL;
  3029. reg = i915_find_fence_reg(dev);
  3030. if (IS_ERR(reg))
  3031. return PTR_ERR(reg);
  3032. if (reg->obj) {
  3033. struct drm_i915_gem_object *old = reg->obj;
  3034. ret = i915_gem_object_wait_fence(old);
  3035. if (ret)
  3036. return ret;
  3037. i915_gem_object_fence_lost(old);
  3038. }
  3039. } else
  3040. return 0;
  3041. i915_gem_object_update_fence(obj, reg, enable);
  3042. return 0;
  3043. }
  3044. static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
  3045. unsigned long cache_level)
  3046. {
  3047. struct drm_mm_node *gtt_space = &vma->node;
  3048. struct drm_mm_node *other;
  3049. /*
  3050. * On some machines we have to be careful when putting differing types
  3051. * of snoopable memory together to avoid the prefetcher crossing memory
  3052. * domains and dying. During vm initialisation, we decide whether or not
  3053. * these constraints apply and set the drm_mm.color_adjust
  3054. * appropriately.
  3055. */
  3056. if (vma->vm->mm.color_adjust == NULL)
  3057. return true;
  3058. if (!drm_mm_node_allocated(gtt_space))
  3059. return true;
  3060. if (list_empty(&gtt_space->node_list))
  3061. return true;
  3062. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  3063. if (other->allocated && !other->hole_follows && other->color != cache_level)
  3064. return false;
  3065. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  3066. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  3067. return false;
  3068. return true;
  3069. }
  3070. /**
  3071. * Finds free space in the GTT aperture and binds the object or a view of it
  3072. * there.
  3073. */
  3074. static struct i915_vma *
  3075. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  3076. struct i915_address_space *vm,
  3077. const struct i915_ggtt_view *ggtt_view,
  3078. unsigned alignment,
  3079. uint64_t flags)
  3080. {
  3081. struct drm_device *dev = obj->base.dev;
  3082. struct drm_i915_private *dev_priv = dev->dev_private;
  3083. u32 size, fence_size, fence_alignment, unfenced_alignment;
  3084. unsigned long start =
  3085. flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  3086. unsigned long end =
  3087. flags & PIN_MAPPABLE ? dev_priv->gtt.mappable_end : vm->total;
  3088. struct i915_vma *vma;
  3089. int ret;
  3090. if (i915_is_ggtt(vm)) {
  3091. u32 view_size;
  3092. if (WARN_ON(!ggtt_view))
  3093. return ERR_PTR(-EINVAL);
  3094. view_size = i915_ggtt_view_size(obj, ggtt_view);
  3095. fence_size = i915_gem_get_gtt_size(dev,
  3096. view_size,
  3097. obj->tiling_mode);
  3098. fence_alignment = i915_gem_get_gtt_alignment(dev,
  3099. view_size,
  3100. obj->tiling_mode,
  3101. true);
  3102. unfenced_alignment = i915_gem_get_gtt_alignment(dev,
  3103. view_size,
  3104. obj->tiling_mode,
  3105. false);
  3106. size = flags & PIN_MAPPABLE ? fence_size : view_size;
  3107. } else {
  3108. fence_size = i915_gem_get_gtt_size(dev,
  3109. obj->base.size,
  3110. obj->tiling_mode);
  3111. fence_alignment = i915_gem_get_gtt_alignment(dev,
  3112. obj->base.size,
  3113. obj->tiling_mode,
  3114. true);
  3115. unfenced_alignment =
  3116. i915_gem_get_gtt_alignment(dev,
  3117. obj->base.size,
  3118. obj->tiling_mode,
  3119. false);
  3120. size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
  3121. }
  3122. if (alignment == 0)
  3123. alignment = flags & PIN_MAPPABLE ? fence_alignment :
  3124. unfenced_alignment;
  3125. if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
  3126. DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
  3127. ggtt_view ? ggtt_view->type : 0,
  3128. alignment);
  3129. return ERR_PTR(-EINVAL);
  3130. }
  3131. /* If binding the object/GGTT view requires more space than the entire
  3132. * aperture has, reject it early before evicting everything in a vain
  3133. * attempt to find space.
  3134. */
  3135. if (size > end) {
  3136. DRM_DEBUG("Attempting to bind an object (view type=%u) larger than the aperture: size=%u > %s aperture=%lu\n",
  3137. ggtt_view ? ggtt_view->type : 0,
  3138. size,
  3139. flags & PIN_MAPPABLE ? "mappable" : "total",
  3140. end);
  3141. return ERR_PTR(-E2BIG);
  3142. }
  3143. ret = i915_gem_object_get_pages(obj);
  3144. if (ret)
  3145. return ERR_PTR(ret);
  3146. i915_gem_object_pin_pages(obj);
  3147. vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
  3148. i915_gem_obj_lookup_or_create_vma(obj, vm);
  3149. if (IS_ERR(vma))
  3150. goto err_unpin;
  3151. search_free:
  3152. ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  3153. size, alignment,
  3154. obj->cache_level,
  3155. start, end,
  3156. DRM_MM_SEARCH_DEFAULT,
  3157. DRM_MM_CREATE_DEFAULT);
  3158. if (ret) {
  3159. ret = i915_gem_evict_something(dev, vm, size, alignment,
  3160. obj->cache_level,
  3161. start, end,
  3162. flags);
  3163. if (ret == 0)
  3164. goto search_free;
  3165. goto err_free_vma;
  3166. }
  3167. if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
  3168. ret = -EINVAL;
  3169. goto err_remove_node;
  3170. }
  3171. ret = i915_gem_gtt_prepare_object(obj);
  3172. if (ret)
  3173. goto err_remove_node;
  3174. trace_i915_vma_bind(vma, flags);
  3175. ret = i915_vma_bind(vma, obj->cache_level, flags);
  3176. if (ret)
  3177. goto err_finish_gtt;
  3178. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  3179. list_add_tail(&vma->mm_list, &vm->inactive_list);
  3180. return vma;
  3181. err_finish_gtt:
  3182. i915_gem_gtt_finish_object(obj);
  3183. err_remove_node:
  3184. drm_mm_remove_node(&vma->node);
  3185. err_free_vma:
  3186. i915_gem_vma_destroy(vma);
  3187. vma = ERR_PTR(ret);
  3188. err_unpin:
  3189. i915_gem_object_unpin_pages(obj);
  3190. return vma;
  3191. }
  3192. bool
  3193. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  3194. bool force)
  3195. {
  3196. /* If we don't have a page list set up, then we're not pinned
  3197. * to GPU, and we can ignore the cache flush because it'll happen
  3198. * again at bind time.
  3199. */
  3200. if (obj->pages == NULL)
  3201. return false;
  3202. /*
  3203. * Stolen memory is always coherent with the GPU as it is explicitly
  3204. * marked as wc by the system, or the system is cache-coherent.
  3205. */
  3206. if (obj->stolen || obj->phys_handle)
  3207. return false;
  3208. /* If the GPU is snooping the contents of the CPU cache,
  3209. * we do not need to manually clear the CPU cache lines. However,
  3210. * the caches are only snooped when the render cache is
  3211. * flushed/invalidated. As we always have to emit invalidations
  3212. * and flushes when moving into and out of the RENDER domain, correct
  3213. * snooping behaviour occurs naturally as the result of our domain
  3214. * tracking.
  3215. */
  3216. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
  3217. obj->cache_dirty = true;
  3218. return false;
  3219. }
  3220. trace_i915_gem_object_clflush(obj);
  3221. drm_clflush_sg(obj->pages);
  3222. obj->cache_dirty = false;
  3223. return true;
  3224. }
  3225. /** Flushes the GTT write domain for the object if it's dirty. */
  3226. static void
  3227. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  3228. {
  3229. uint32_t old_write_domain;
  3230. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  3231. return;
  3232. /* No actual flushing is required for the GTT write domain. Writes
  3233. * to it immediately go to main memory as far as we know, so there's
  3234. * no chipset flush. It also doesn't land in render cache.
  3235. *
  3236. * However, we do have to enforce the order so that all writes through
  3237. * the GTT land before any writes to the device, such as updates to
  3238. * the GATT itself.
  3239. */
  3240. wmb();
  3241. old_write_domain = obj->base.write_domain;
  3242. obj->base.write_domain = 0;
  3243. intel_fb_obj_flush(obj, false);
  3244. trace_i915_gem_object_change_domain(obj,
  3245. obj->base.read_domains,
  3246. old_write_domain);
  3247. }
  3248. /** Flushes the CPU write domain for the object if it's dirty. */
  3249. static void
  3250. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  3251. {
  3252. uint32_t old_write_domain;
  3253. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  3254. return;
  3255. if (i915_gem_clflush_object(obj, obj->pin_display))
  3256. i915_gem_chipset_flush(obj->base.dev);
  3257. old_write_domain = obj->base.write_domain;
  3258. obj->base.write_domain = 0;
  3259. intel_fb_obj_flush(obj, false);
  3260. trace_i915_gem_object_change_domain(obj,
  3261. obj->base.read_domains,
  3262. old_write_domain);
  3263. }
  3264. /**
  3265. * Moves a single object to the GTT read, and possibly write domain.
  3266. *
  3267. * This function returns when the move is complete, including waiting on
  3268. * flushes to occur.
  3269. */
  3270. int
  3271. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  3272. {
  3273. uint32_t old_write_domain, old_read_domains;
  3274. struct i915_vma *vma;
  3275. int ret;
  3276. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  3277. return 0;
  3278. ret = i915_gem_object_wait_rendering(obj, !write);
  3279. if (ret)
  3280. return ret;
  3281. /* Flush and acquire obj->pages so that we are coherent through
  3282. * direct access in memory with previous cached writes through
  3283. * shmemfs and that our cache domain tracking remains valid.
  3284. * For example, if the obj->filp was moved to swap without us
  3285. * being notified and releasing the pages, we would mistakenly
  3286. * continue to assume that the obj remained out of the CPU cached
  3287. * domain.
  3288. */
  3289. ret = i915_gem_object_get_pages(obj);
  3290. if (ret)
  3291. return ret;
  3292. i915_gem_object_flush_cpu_write_domain(obj);
  3293. /* Serialise direct access to this object with the barriers for
  3294. * coherent writes from the GPU, by effectively invalidating the
  3295. * GTT domain upon first access.
  3296. */
  3297. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3298. mb();
  3299. old_write_domain = obj->base.write_domain;
  3300. old_read_domains = obj->base.read_domains;
  3301. /* It should now be out of any other write domains, and we can update
  3302. * the domain values for our changes.
  3303. */
  3304. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  3305. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3306. if (write) {
  3307. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  3308. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  3309. obj->dirty = 1;
  3310. }
  3311. if (write)
  3312. intel_fb_obj_invalidate(obj, ORIGIN_GTT);
  3313. trace_i915_gem_object_change_domain(obj,
  3314. old_read_domains,
  3315. old_write_domain);
  3316. /* And bump the LRU for this access */
  3317. vma = i915_gem_obj_to_ggtt(obj);
  3318. if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
  3319. list_move_tail(&vma->mm_list,
  3320. &to_i915(obj->base.dev)->gtt.base.inactive_list);
  3321. return 0;
  3322. }
  3323. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3324. enum i915_cache_level cache_level)
  3325. {
  3326. struct drm_device *dev = obj->base.dev;
  3327. struct i915_vma *vma, *next;
  3328. int ret;
  3329. if (obj->cache_level == cache_level)
  3330. return 0;
  3331. if (i915_gem_obj_is_pinned(obj)) {
  3332. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3333. return -EBUSY;
  3334. }
  3335. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3336. if (!i915_gem_valid_gtt_space(vma, cache_level)) {
  3337. ret = i915_vma_unbind(vma);
  3338. if (ret)
  3339. return ret;
  3340. }
  3341. }
  3342. if (i915_gem_obj_bound_any(obj)) {
  3343. ret = i915_gem_object_wait_rendering(obj, false);
  3344. if (ret)
  3345. return ret;
  3346. i915_gem_object_finish_gtt(obj);
  3347. /* Before SandyBridge, you could not use tiling or fence
  3348. * registers with snooped memory, so relinquish any fences
  3349. * currently pointing to our region in the aperture.
  3350. */
  3351. if (INTEL_INFO(dev)->gen < 6) {
  3352. ret = i915_gem_object_put_fence(obj);
  3353. if (ret)
  3354. return ret;
  3355. }
  3356. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3357. if (drm_mm_node_allocated(&vma->node)) {
  3358. ret = i915_vma_bind(vma, cache_level,
  3359. PIN_UPDATE);
  3360. if (ret)
  3361. return ret;
  3362. }
  3363. }
  3364. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3365. vma->node.color = cache_level;
  3366. obj->cache_level = cache_level;
  3367. if (obj->cache_dirty &&
  3368. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  3369. cpu_write_needs_clflush(obj)) {
  3370. if (i915_gem_clflush_object(obj, true))
  3371. i915_gem_chipset_flush(obj->base.dev);
  3372. }
  3373. return 0;
  3374. }
  3375. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3376. struct drm_file *file)
  3377. {
  3378. struct drm_i915_gem_caching *args = data;
  3379. struct drm_i915_gem_object *obj;
  3380. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3381. if (&obj->base == NULL)
  3382. return -ENOENT;
  3383. switch (obj->cache_level) {
  3384. case I915_CACHE_LLC:
  3385. case I915_CACHE_L3_LLC:
  3386. args->caching = I915_CACHING_CACHED;
  3387. break;
  3388. case I915_CACHE_WT:
  3389. args->caching = I915_CACHING_DISPLAY;
  3390. break;
  3391. default:
  3392. args->caching = I915_CACHING_NONE;
  3393. break;
  3394. }
  3395. drm_gem_object_unreference_unlocked(&obj->base);
  3396. return 0;
  3397. }
  3398. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3399. struct drm_file *file)
  3400. {
  3401. struct drm_i915_gem_caching *args = data;
  3402. struct drm_i915_gem_object *obj;
  3403. enum i915_cache_level level;
  3404. int ret;
  3405. switch (args->caching) {
  3406. case I915_CACHING_NONE:
  3407. level = I915_CACHE_NONE;
  3408. break;
  3409. case I915_CACHING_CACHED:
  3410. level = I915_CACHE_LLC;
  3411. break;
  3412. case I915_CACHING_DISPLAY:
  3413. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  3414. break;
  3415. default:
  3416. return -EINVAL;
  3417. }
  3418. ret = i915_mutex_lock_interruptible(dev);
  3419. if (ret)
  3420. return ret;
  3421. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3422. if (&obj->base == NULL) {
  3423. ret = -ENOENT;
  3424. goto unlock;
  3425. }
  3426. ret = i915_gem_object_set_cache_level(obj, level);
  3427. drm_gem_object_unreference(&obj->base);
  3428. unlock:
  3429. mutex_unlock(&dev->struct_mutex);
  3430. return ret;
  3431. }
  3432. /*
  3433. * Prepare buffer for display plane (scanout, cursors, etc).
  3434. * Can be called from an uninterruptible phase (modesetting) and allows
  3435. * any flushes to be pipelined (for pageflips).
  3436. */
  3437. int
  3438. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3439. u32 alignment,
  3440. struct intel_engine_cs *pipelined,
  3441. struct drm_i915_gem_request **pipelined_request,
  3442. const struct i915_ggtt_view *view)
  3443. {
  3444. u32 old_read_domains, old_write_domain;
  3445. int ret;
  3446. ret = i915_gem_object_sync(obj, pipelined, pipelined_request);
  3447. if (ret)
  3448. return ret;
  3449. /* Mark the pin_display early so that we account for the
  3450. * display coherency whilst setting up the cache domains.
  3451. */
  3452. obj->pin_display++;
  3453. /* The display engine is not coherent with the LLC cache on gen6. As
  3454. * a result, we make sure that the pinning that is about to occur is
  3455. * done with uncached PTEs. This is lowest common denominator for all
  3456. * chipsets.
  3457. *
  3458. * However for gen6+, we could do better by using the GFDT bit instead
  3459. * of uncaching, which would allow us to flush all the LLC-cached data
  3460. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3461. */
  3462. ret = i915_gem_object_set_cache_level(obj,
  3463. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  3464. if (ret)
  3465. goto err_unpin_display;
  3466. /* As the user may map the buffer once pinned in the display plane
  3467. * (e.g. libkms for the bootup splash), we have to ensure that we
  3468. * always use map_and_fenceable for all scanout buffers.
  3469. */
  3470. ret = i915_gem_object_ggtt_pin(obj, view, alignment,
  3471. view->type == I915_GGTT_VIEW_NORMAL ?
  3472. PIN_MAPPABLE : 0);
  3473. if (ret)
  3474. goto err_unpin_display;
  3475. i915_gem_object_flush_cpu_write_domain(obj);
  3476. old_write_domain = obj->base.write_domain;
  3477. old_read_domains = obj->base.read_domains;
  3478. /* It should now be out of any other write domains, and we can update
  3479. * the domain values for our changes.
  3480. */
  3481. obj->base.write_domain = 0;
  3482. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3483. trace_i915_gem_object_change_domain(obj,
  3484. old_read_domains,
  3485. old_write_domain);
  3486. return 0;
  3487. err_unpin_display:
  3488. obj->pin_display--;
  3489. return ret;
  3490. }
  3491. void
  3492. i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
  3493. const struct i915_ggtt_view *view)
  3494. {
  3495. if (WARN_ON(obj->pin_display == 0))
  3496. return;
  3497. i915_gem_object_ggtt_unpin_view(obj, view);
  3498. obj->pin_display--;
  3499. }
  3500. /**
  3501. * Moves a single object to the CPU read, and possibly write domain.
  3502. *
  3503. * This function returns when the move is complete, including waiting on
  3504. * flushes to occur.
  3505. */
  3506. int
  3507. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3508. {
  3509. uint32_t old_write_domain, old_read_domains;
  3510. int ret;
  3511. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3512. return 0;
  3513. ret = i915_gem_object_wait_rendering(obj, !write);
  3514. if (ret)
  3515. return ret;
  3516. i915_gem_object_flush_gtt_write_domain(obj);
  3517. old_write_domain = obj->base.write_domain;
  3518. old_read_domains = obj->base.read_domains;
  3519. /* Flush the CPU cache if it's still invalid. */
  3520. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3521. i915_gem_clflush_object(obj, false);
  3522. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3523. }
  3524. /* It should now be out of any other write domains, and we can update
  3525. * the domain values for our changes.
  3526. */
  3527. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3528. /* If we're writing through the CPU, then the GPU read domains will
  3529. * need to be invalidated at next use.
  3530. */
  3531. if (write) {
  3532. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3533. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3534. }
  3535. if (write)
  3536. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  3537. trace_i915_gem_object_change_domain(obj,
  3538. old_read_domains,
  3539. old_write_domain);
  3540. return 0;
  3541. }
  3542. /* Throttle our rendering by waiting until the ring has completed our requests
  3543. * emitted over 20 msec ago.
  3544. *
  3545. * Note that if we were to use the current jiffies each time around the loop,
  3546. * we wouldn't escape the function with any frames outstanding if the time to
  3547. * render a frame was over 20ms.
  3548. *
  3549. * This should get us reasonable parallelism between CPU and GPU but also
  3550. * relatively low latency when blocking on a particular request to finish.
  3551. */
  3552. static int
  3553. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3554. {
  3555. struct drm_i915_private *dev_priv = dev->dev_private;
  3556. struct drm_i915_file_private *file_priv = file->driver_priv;
  3557. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3558. struct drm_i915_gem_request *request, *target = NULL;
  3559. unsigned reset_counter;
  3560. int ret;
  3561. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3562. if (ret)
  3563. return ret;
  3564. ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  3565. if (ret)
  3566. return ret;
  3567. spin_lock(&file_priv->mm.lock);
  3568. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3569. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3570. break;
  3571. target = request;
  3572. }
  3573. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  3574. if (target)
  3575. i915_gem_request_reference(target);
  3576. spin_unlock(&file_priv->mm.lock);
  3577. if (target == NULL)
  3578. return 0;
  3579. ret = __i915_wait_request(target, reset_counter, true, NULL, NULL);
  3580. if (ret == 0)
  3581. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3582. i915_gem_request_unreference__unlocked(target);
  3583. return ret;
  3584. }
  3585. static bool
  3586. i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
  3587. {
  3588. struct drm_i915_gem_object *obj = vma->obj;
  3589. if (alignment &&
  3590. vma->node.start & (alignment - 1))
  3591. return true;
  3592. if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
  3593. return true;
  3594. if (flags & PIN_OFFSET_BIAS &&
  3595. vma->node.start < (flags & PIN_OFFSET_MASK))
  3596. return true;
  3597. return false;
  3598. }
  3599. static int
  3600. i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
  3601. struct i915_address_space *vm,
  3602. const struct i915_ggtt_view *ggtt_view,
  3603. uint32_t alignment,
  3604. uint64_t flags)
  3605. {
  3606. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3607. struct i915_vma *vma;
  3608. unsigned bound;
  3609. int ret;
  3610. if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
  3611. return -ENODEV;
  3612. if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
  3613. return -EINVAL;
  3614. if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
  3615. return -EINVAL;
  3616. if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
  3617. return -EINVAL;
  3618. vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
  3619. i915_gem_obj_to_vma(obj, vm);
  3620. if (IS_ERR(vma))
  3621. return PTR_ERR(vma);
  3622. if (vma) {
  3623. if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3624. return -EBUSY;
  3625. if (i915_vma_misplaced(vma, alignment, flags)) {
  3626. unsigned long offset;
  3627. offset = ggtt_view ? i915_gem_obj_ggtt_offset_view(obj, ggtt_view) :
  3628. i915_gem_obj_offset(obj, vm);
  3629. WARN(vma->pin_count,
  3630. "bo is already pinned in %s with incorrect alignment:"
  3631. " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
  3632. " obj->map_and_fenceable=%d\n",
  3633. ggtt_view ? "ggtt" : "ppgtt",
  3634. offset,
  3635. alignment,
  3636. !!(flags & PIN_MAPPABLE),
  3637. obj->map_and_fenceable);
  3638. ret = i915_vma_unbind(vma);
  3639. if (ret)
  3640. return ret;
  3641. vma = NULL;
  3642. }
  3643. }
  3644. bound = vma ? vma->bound : 0;
  3645. if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
  3646. vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
  3647. flags);
  3648. if (IS_ERR(vma))
  3649. return PTR_ERR(vma);
  3650. } else {
  3651. ret = i915_vma_bind(vma, obj->cache_level, flags);
  3652. if (ret)
  3653. return ret;
  3654. }
  3655. if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
  3656. (bound ^ vma->bound) & GLOBAL_BIND) {
  3657. bool mappable, fenceable;
  3658. u32 fence_size, fence_alignment;
  3659. fence_size = i915_gem_get_gtt_size(obj->base.dev,
  3660. obj->base.size,
  3661. obj->tiling_mode);
  3662. fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
  3663. obj->base.size,
  3664. obj->tiling_mode,
  3665. true);
  3666. fenceable = (vma->node.size == fence_size &&
  3667. (vma->node.start & (fence_alignment - 1)) == 0);
  3668. mappable = (vma->node.start + fence_size <=
  3669. dev_priv->gtt.mappable_end);
  3670. obj->map_and_fenceable = mappable && fenceable;
  3671. WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
  3672. }
  3673. vma->pin_count++;
  3674. return 0;
  3675. }
  3676. int
  3677. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3678. struct i915_address_space *vm,
  3679. uint32_t alignment,
  3680. uint64_t flags)
  3681. {
  3682. return i915_gem_object_do_pin(obj, vm,
  3683. i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
  3684. alignment, flags);
  3685. }
  3686. int
  3687. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3688. const struct i915_ggtt_view *view,
  3689. uint32_t alignment,
  3690. uint64_t flags)
  3691. {
  3692. if (WARN_ONCE(!view, "no view specified"))
  3693. return -EINVAL;
  3694. return i915_gem_object_do_pin(obj, i915_obj_to_ggtt(obj), view,
  3695. alignment, flags | PIN_GLOBAL);
  3696. }
  3697. void
  3698. i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
  3699. const struct i915_ggtt_view *view)
  3700. {
  3701. struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
  3702. BUG_ON(!vma);
  3703. WARN_ON(vma->pin_count == 0);
  3704. WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
  3705. --vma->pin_count;
  3706. }
  3707. bool
  3708. i915_gem_object_pin_fence(struct drm_i915_gem_object *obj)
  3709. {
  3710. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  3711. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3712. struct i915_vma *ggtt_vma = i915_gem_obj_to_ggtt(obj);
  3713. WARN_ON(!ggtt_vma ||
  3714. dev_priv->fence_regs[obj->fence_reg].pin_count >
  3715. ggtt_vma->pin_count);
  3716. dev_priv->fence_regs[obj->fence_reg].pin_count++;
  3717. return true;
  3718. } else
  3719. return false;
  3720. }
  3721. void
  3722. i915_gem_object_unpin_fence(struct drm_i915_gem_object *obj)
  3723. {
  3724. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  3725. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3726. WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count <= 0);
  3727. dev_priv->fence_regs[obj->fence_reg].pin_count--;
  3728. }
  3729. }
  3730. int
  3731. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3732. struct drm_file *file)
  3733. {
  3734. struct drm_i915_gem_busy *args = data;
  3735. struct drm_i915_gem_object *obj;
  3736. int ret;
  3737. ret = i915_mutex_lock_interruptible(dev);
  3738. if (ret)
  3739. return ret;
  3740. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3741. if (&obj->base == NULL) {
  3742. ret = -ENOENT;
  3743. goto unlock;
  3744. }
  3745. /* Count all active objects as busy, even if they are currently not used
  3746. * by the gpu. Users of this interface expect objects to eventually
  3747. * become non-busy without any further actions, therefore emit any
  3748. * necessary flushes here.
  3749. */
  3750. ret = i915_gem_object_flush_active(obj);
  3751. if (ret)
  3752. goto unref;
  3753. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3754. args->busy = obj->active << 16;
  3755. if (obj->last_write_req)
  3756. args->busy |= obj->last_write_req->ring->id;
  3757. unref:
  3758. drm_gem_object_unreference(&obj->base);
  3759. unlock:
  3760. mutex_unlock(&dev->struct_mutex);
  3761. return ret;
  3762. }
  3763. int
  3764. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3765. struct drm_file *file_priv)
  3766. {
  3767. return i915_gem_ring_throttle(dev, file_priv);
  3768. }
  3769. int
  3770. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3771. struct drm_file *file_priv)
  3772. {
  3773. struct drm_i915_private *dev_priv = dev->dev_private;
  3774. struct drm_i915_gem_madvise *args = data;
  3775. struct drm_i915_gem_object *obj;
  3776. int ret;
  3777. switch (args->madv) {
  3778. case I915_MADV_DONTNEED:
  3779. case I915_MADV_WILLNEED:
  3780. break;
  3781. default:
  3782. return -EINVAL;
  3783. }
  3784. ret = i915_mutex_lock_interruptible(dev);
  3785. if (ret)
  3786. return ret;
  3787. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3788. if (&obj->base == NULL) {
  3789. ret = -ENOENT;
  3790. goto unlock;
  3791. }
  3792. if (i915_gem_obj_is_pinned(obj)) {
  3793. ret = -EINVAL;
  3794. goto out;
  3795. }
  3796. if (obj->pages &&
  3797. obj->tiling_mode != I915_TILING_NONE &&
  3798. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3799. if (obj->madv == I915_MADV_WILLNEED)
  3800. i915_gem_object_unpin_pages(obj);
  3801. if (args->madv == I915_MADV_WILLNEED)
  3802. i915_gem_object_pin_pages(obj);
  3803. }
  3804. if (obj->madv != __I915_MADV_PURGED)
  3805. obj->madv = args->madv;
  3806. /* if the object is no longer attached, discard its backing storage */
  3807. if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
  3808. i915_gem_object_truncate(obj);
  3809. args->retained = obj->madv != __I915_MADV_PURGED;
  3810. out:
  3811. drm_gem_object_unreference(&obj->base);
  3812. unlock:
  3813. mutex_unlock(&dev->struct_mutex);
  3814. return ret;
  3815. }
  3816. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3817. const struct drm_i915_gem_object_ops *ops)
  3818. {
  3819. int i;
  3820. INIT_LIST_HEAD(&obj->global_list);
  3821. for (i = 0; i < I915_NUM_RINGS; i++)
  3822. INIT_LIST_HEAD(&obj->ring_list[i]);
  3823. INIT_LIST_HEAD(&obj->obj_exec_link);
  3824. INIT_LIST_HEAD(&obj->vma_list);
  3825. INIT_LIST_HEAD(&obj->batch_pool_link);
  3826. obj->ops = ops;
  3827. obj->fence_reg = I915_FENCE_REG_NONE;
  3828. obj->madv = I915_MADV_WILLNEED;
  3829. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3830. }
  3831. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3832. .get_pages = i915_gem_object_get_pages_gtt,
  3833. .put_pages = i915_gem_object_put_pages_gtt,
  3834. };
  3835. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3836. size_t size)
  3837. {
  3838. struct drm_i915_gem_object *obj;
  3839. struct address_space *mapping;
  3840. gfp_t mask;
  3841. obj = i915_gem_object_alloc(dev);
  3842. if (obj == NULL)
  3843. return NULL;
  3844. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3845. i915_gem_object_free(obj);
  3846. return NULL;
  3847. }
  3848. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3849. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3850. /* 965gm cannot relocate objects above 4GiB. */
  3851. mask &= ~__GFP_HIGHMEM;
  3852. mask |= __GFP_DMA32;
  3853. }
  3854. mapping = file_inode(obj->base.filp)->i_mapping;
  3855. mapping_set_gfp_mask(mapping, mask);
  3856. i915_gem_object_init(obj, &i915_gem_object_ops);
  3857. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3858. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3859. if (HAS_LLC(dev)) {
  3860. /* On some devices, we can have the GPU use the LLC (the CPU
  3861. * cache) for about a 10% performance improvement
  3862. * compared to uncached. Graphics requests other than
  3863. * display scanout are coherent with the CPU in
  3864. * accessing this cache. This means in this mode we
  3865. * don't need to clflush on the CPU side, and on the
  3866. * GPU side we only need to flush internal caches to
  3867. * get data visible to the CPU.
  3868. *
  3869. * However, we maintain the display planes as UC, and so
  3870. * need to rebind when first used as such.
  3871. */
  3872. obj->cache_level = I915_CACHE_LLC;
  3873. } else
  3874. obj->cache_level = I915_CACHE_NONE;
  3875. trace_i915_gem_object_create(obj);
  3876. return obj;
  3877. }
  3878. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3879. {
  3880. /* If we are the last user of the backing storage (be it shmemfs
  3881. * pages or stolen etc), we know that the pages are going to be
  3882. * immediately released. In this case, we can then skip copying
  3883. * back the contents from the GPU.
  3884. */
  3885. if (obj->madv != I915_MADV_WILLNEED)
  3886. return false;
  3887. if (obj->base.filp == NULL)
  3888. return true;
  3889. /* At first glance, this looks racy, but then again so would be
  3890. * userspace racing mmap against close. However, the first external
  3891. * reference to the filp can only be obtained through the
  3892. * i915_gem_mmap_ioctl() which safeguards us against the user
  3893. * acquiring such a reference whilst we are in the middle of
  3894. * freeing the object.
  3895. */
  3896. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3897. }
  3898. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3899. {
  3900. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3901. struct drm_device *dev = obj->base.dev;
  3902. struct drm_i915_private *dev_priv = dev->dev_private;
  3903. struct i915_vma *vma, *next;
  3904. intel_runtime_pm_get(dev_priv);
  3905. trace_i915_gem_object_destroy(obj);
  3906. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3907. int ret;
  3908. vma->pin_count = 0;
  3909. ret = i915_vma_unbind(vma);
  3910. if (WARN_ON(ret == -ERESTARTSYS)) {
  3911. bool was_interruptible;
  3912. was_interruptible = dev_priv->mm.interruptible;
  3913. dev_priv->mm.interruptible = false;
  3914. WARN_ON(i915_vma_unbind(vma));
  3915. dev_priv->mm.interruptible = was_interruptible;
  3916. }
  3917. }
  3918. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3919. * before progressing. */
  3920. if (obj->stolen)
  3921. i915_gem_object_unpin_pages(obj);
  3922. WARN_ON(obj->frontbuffer_bits);
  3923. if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
  3924. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
  3925. obj->tiling_mode != I915_TILING_NONE)
  3926. i915_gem_object_unpin_pages(obj);
  3927. if (WARN_ON(obj->pages_pin_count))
  3928. obj->pages_pin_count = 0;
  3929. if (discard_backing_storage(obj))
  3930. obj->madv = I915_MADV_DONTNEED;
  3931. i915_gem_object_put_pages(obj);
  3932. i915_gem_object_free_mmap_offset(obj);
  3933. BUG_ON(obj->pages);
  3934. if (obj->base.import_attach)
  3935. drm_prime_gem_destroy(&obj->base, NULL);
  3936. if (obj->ops->release)
  3937. obj->ops->release(obj);
  3938. drm_gem_object_release(&obj->base);
  3939. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3940. kfree(obj->bit_17);
  3941. i915_gem_object_free(obj);
  3942. intel_runtime_pm_put(dev_priv);
  3943. }
  3944. struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
  3945. struct i915_address_space *vm)
  3946. {
  3947. struct i915_vma *vma;
  3948. list_for_each_entry(vma, &obj->vma_list, vma_link) {
  3949. if (i915_is_ggtt(vma->vm) &&
  3950. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  3951. continue;
  3952. if (vma->vm == vm)
  3953. return vma;
  3954. }
  3955. return NULL;
  3956. }
  3957. struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
  3958. const struct i915_ggtt_view *view)
  3959. {
  3960. struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
  3961. struct i915_vma *vma;
  3962. if (WARN_ONCE(!view, "no view specified"))
  3963. return ERR_PTR(-EINVAL);
  3964. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3965. if (vma->vm == ggtt &&
  3966. i915_ggtt_view_equal(&vma->ggtt_view, view))
  3967. return vma;
  3968. return NULL;
  3969. }
  3970. void i915_gem_vma_destroy(struct i915_vma *vma)
  3971. {
  3972. struct i915_address_space *vm = NULL;
  3973. WARN_ON(vma->node.allocated);
  3974. /* Keep the vma as a placeholder in the execbuffer reservation lists */
  3975. if (!list_empty(&vma->exec_list))
  3976. return;
  3977. vm = vma->vm;
  3978. if (!i915_is_ggtt(vm))
  3979. i915_ppgtt_put(i915_vm_to_ppgtt(vm));
  3980. list_del(&vma->vma_link);
  3981. kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
  3982. }
  3983. static void
  3984. i915_gem_stop_ringbuffers(struct drm_device *dev)
  3985. {
  3986. struct drm_i915_private *dev_priv = dev->dev_private;
  3987. struct intel_engine_cs *ring;
  3988. int i;
  3989. for_each_ring(ring, dev_priv, i)
  3990. dev_priv->gt.stop_ring(ring);
  3991. }
  3992. int
  3993. i915_gem_suspend(struct drm_device *dev)
  3994. {
  3995. struct drm_i915_private *dev_priv = dev->dev_private;
  3996. int ret = 0;
  3997. mutex_lock(&dev->struct_mutex);
  3998. ret = i915_gpu_idle(dev);
  3999. if (ret)
  4000. goto err;
  4001. i915_gem_retire_requests(dev);
  4002. i915_gem_stop_ringbuffers(dev);
  4003. mutex_unlock(&dev->struct_mutex);
  4004. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  4005. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  4006. flush_delayed_work(&dev_priv->mm.idle_work);
  4007. /* Assert that we sucessfully flushed all the work and
  4008. * reset the GPU back to its idle, low power state.
  4009. */
  4010. WARN_ON(dev_priv->mm.busy);
  4011. return 0;
  4012. err:
  4013. mutex_unlock(&dev->struct_mutex);
  4014. return ret;
  4015. }
  4016. int i915_gem_l3_remap(struct drm_i915_gem_request *req, int slice)
  4017. {
  4018. struct intel_engine_cs *ring = req->ring;
  4019. struct drm_device *dev = ring->dev;
  4020. struct drm_i915_private *dev_priv = dev->dev_private;
  4021. u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200);
  4022. u32 *remap_info = dev_priv->l3_parity.remap_info[slice];
  4023. int i, ret;
  4024. if (!HAS_L3_DPF(dev) || !remap_info)
  4025. return 0;
  4026. ret = intel_ring_begin(ring, GEN7_L3LOG_SIZE / 4 * 3);
  4027. if (ret)
  4028. return ret;
  4029. /*
  4030. * Note: We do not worry about the concurrent register cacheline hang
  4031. * here because no other code should access these registers other than
  4032. * at initialization time.
  4033. */
  4034. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  4035. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  4036. intel_ring_emit(ring, reg_base + i);
  4037. intel_ring_emit(ring, remap_info[i/4]);
  4038. }
  4039. intel_ring_advance(ring);
  4040. return ret;
  4041. }
  4042. void i915_gem_init_swizzling(struct drm_device *dev)
  4043. {
  4044. struct drm_i915_private *dev_priv = dev->dev_private;
  4045. if (INTEL_INFO(dev)->gen < 5 ||
  4046. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  4047. return;
  4048. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  4049. DISP_TILE_SURFACE_SWIZZLING);
  4050. if (IS_GEN5(dev))
  4051. return;
  4052. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  4053. if (IS_GEN6(dev))
  4054. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  4055. else if (IS_GEN7(dev))
  4056. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  4057. else if (IS_GEN8(dev))
  4058. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  4059. else
  4060. BUG();
  4061. }
  4062. static bool
  4063. intel_enable_blt(struct drm_device *dev)
  4064. {
  4065. if (!HAS_BLT(dev))
  4066. return false;
  4067. /* The blitter was dysfunctional on early prototypes */
  4068. if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  4069. DRM_INFO("BLT not supported on this pre-production hardware;"
  4070. " graphics performance will be degraded.\n");
  4071. return false;
  4072. }
  4073. return true;
  4074. }
  4075. static void init_unused_ring(struct drm_device *dev, u32 base)
  4076. {
  4077. struct drm_i915_private *dev_priv = dev->dev_private;
  4078. I915_WRITE(RING_CTL(base), 0);
  4079. I915_WRITE(RING_HEAD(base), 0);
  4080. I915_WRITE(RING_TAIL(base), 0);
  4081. I915_WRITE(RING_START(base), 0);
  4082. }
  4083. static void init_unused_rings(struct drm_device *dev)
  4084. {
  4085. if (IS_I830(dev)) {
  4086. init_unused_ring(dev, PRB1_BASE);
  4087. init_unused_ring(dev, SRB0_BASE);
  4088. init_unused_ring(dev, SRB1_BASE);
  4089. init_unused_ring(dev, SRB2_BASE);
  4090. init_unused_ring(dev, SRB3_BASE);
  4091. } else if (IS_GEN2(dev)) {
  4092. init_unused_ring(dev, SRB0_BASE);
  4093. init_unused_ring(dev, SRB1_BASE);
  4094. } else if (IS_GEN3(dev)) {
  4095. init_unused_ring(dev, PRB1_BASE);
  4096. init_unused_ring(dev, PRB2_BASE);
  4097. }
  4098. }
  4099. int i915_gem_init_rings(struct drm_device *dev)
  4100. {
  4101. struct drm_i915_private *dev_priv = dev->dev_private;
  4102. int ret;
  4103. ret = intel_init_render_ring_buffer(dev);
  4104. if (ret)
  4105. return ret;
  4106. if (HAS_BSD(dev)) {
  4107. ret = intel_init_bsd_ring_buffer(dev);
  4108. if (ret)
  4109. goto cleanup_render_ring;
  4110. }
  4111. if (intel_enable_blt(dev)) {
  4112. ret = intel_init_blt_ring_buffer(dev);
  4113. if (ret)
  4114. goto cleanup_bsd_ring;
  4115. }
  4116. if (HAS_VEBOX(dev)) {
  4117. ret = intel_init_vebox_ring_buffer(dev);
  4118. if (ret)
  4119. goto cleanup_blt_ring;
  4120. }
  4121. if (HAS_BSD2(dev)) {
  4122. ret = intel_init_bsd2_ring_buffer(dev);
  4123. if (ret)
  4124. goto cleanup_vebox_ring;
  4125. }
  4126. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  4127. if (ret)
  4128. goto cleanup_bsd2_ring;
  4129. return 0;
  4130. cleanup_bsd2_ring:
  4131. intel_cleanup_ring_buffer(&dev_priv->ring[VCS2]);
  4132. cleanup_vebox_ring:
  4133. intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
  4134. cleanup_blt_ring:
  4135. intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  4136. cleanup_bsd_ring:
  4137. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  4138. cleanup_render_ring:
  4139. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  4140. return ret;
  4141. }
  4142. int
  4143. i915_gem_init_hw(struct drm_device *dev)
  4144. {
  4145. struct drm_i915_private *dev_priv = dev->dev_private;
  4146. struct intel_engine_cs *ring;
  4147. int ret, i, j;
  4148. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  4149. return -EIO;
  4150. /* Double layer security blanket, see i915_gem_init() */
  4151. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4152. if (dev_priv->ellc_size)
  4153. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  4154. if (IS_HASWELL(dev))
  4155. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  4156. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  4157. if (HAS_PCH_NOP(dev)) {
  4158. if (IS_IVYBRIDGE(dev)) {
  4159. u32 temp = I915_READ(GEN7_MSG_CTL);
  4160. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  4161. I915_WRITE(GEN7_MSG_CTL, temp);
  4162. } else if (INTEL_INFO(dev)->gen >= 7) {
  4163. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  4164. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4165. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  4166. }
  4167. }
  4168. i915_gem_init_swizzling(dev);
  4169. /*
  4170. * At least 830 can leave some of the unused rings
  4171. * "active" (ie. head != tail) after resume which
  4172. * will prevent c3 entry. Makes sure all unused rings
  4173. * are totally idle.
  4174. */
  4175. init_unused_rings(dev);
  4176. BUG_ON(!dev_priv->ring[RCS].default_context);
  4177. ret = i915_ppgtt_init_hw(dev);
  4178. if (ret) {
  4179. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  4180. goto out;
  4181. }
  4182. /* Need to do basic initialisation of all rings first: */
  4183. for_each_ring(ring, dev_priv, i) {
  4184. ret = ring->init_hw(ring);
  4185. if (ret)
  4186. goto out;
  4187. }
  4188. /* Now it is safe to go back round and do everything else: */
  4189. for_each_ring(ring, dev_priv, i) {
  4190. struct drm_i915_gem_request *req;
  4191. WARN_ON(!ring->default_context);
  4192. ret = i915_gem_request_alloc(ring, ring->default_context, &req);
  4193. if (ret) {
  4194. i915_gem_cleanup_ringbuffer(dev);
  4195. goto out;
  4196. }
  4197. if (ring->id == RCS) {
  4198. for (j = 0; j < NUM_L3_SLICES(dev); j++)
  4199. i915_gem_l3_remap(req, j);
  4200. }
  4201. ret = i915_ppgtt_init_ring(req);
  4202. if (ret && ret != -EIO) {
  4203. DRM_ERROR("PPGTT enable ring #%d failed %d\n", i, ret);
  4204. i915_gem_request_cancel(req);
  4205. i915_gem_cleanup_ringbuffer(dev);
  4206. goto out;
  4207. }
  4208. ret = i915_gem_context_enable(req);
  4209. if (ret && ret != -EIO) {
  4210. DRM_ERROR("Context enable ring #%d failed %d\n", i, ret);
  4211. i915_gem_request_cancel(req);
  4212. i915_gem_cleanup_ringbuffer(dev);
  4213. goto out;
  4214. }
  4215. i915_add_request_no_flush(req);
  4216. }
  4217. out:
  4218. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4219. return ret;
  4220. }
  4221. int i915_gem_init(struct drm_device *dev)
  4222. {
  4223. struct drm_i915_private *dev_priv = dev->dev_private;
  4224. int ret;
  4225. i915.enable_execlists = intel_sanitize_enable_execlists(dev,
  4226. i915.enable_execlists);
  4227. mutex_lock(&dev->struct_mutex);
  4228. if (IS_VALLEYVIEW(dev)) {
  4229. /* VLVA0 (potential hack), BIOS isn't actually waking us */
  4230. I915_WRITE(VLV_GTLC_WAKE_CTRL, VLV_GTLC_ALLOWWAKEREQ);
  4231. if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) &
  4232. VLV_GTLC_ALLOWWAKEACK), 10))
  4233. DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  4234. }
  4235. if (!i915.enable_execlists) {
  4236. dev_priv->gt.execbuf_submit = i915_gem_ringbuffer_submission;
  4237. dev_priv->gt.init_rings = i915_gem_init_rings;
  4238. dev_priv->gt.cleanup_ring = intel_cleanup_ring_buffer;
  4239. dev_priv->gt.stop_ring = intel_stop_ring_buffer;
  4240. } else {
  4241. dev_priv->gt.execbuf_submit = intel_execlists_submission;
  4242. dev_priv->gt.init_rings = intel_logical_rings_init;
  4243. dev_priv->gt.cleanup_ring = intel_logical_ring_cleanup;
  4244. dev_priv->gt.stop_ring = intel_logical_ring_stop;
  4245. }
  4246. /* This is just a security blanket to placate dragons.
  4247. * On some systems, we very sporadically observe that the first TLBs
  4248. * used by the CS may be stale, despite us poking the TLB reset. If
  4249. * we hold the forcewake during initialisation these problems
  4250. * just magically go away.
  4251. */
  4252. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4253. ret = i915_gem_init_userptr(dev);
  4254. if (ret)
  4255. goto out_unlock;
  4256. i915_gem_init_global_gtt(dev);
  4257. ret = i915_gem_context_init(dev);
  4258. if (ret)
  4259. goto out_unlock;
  4260. ret = dev_priv->gt.init_rings(dev);
  4261. if (ret)
  4262. goto out_unlock;
  4263. ret = i915_gem_init_hw(dev);
  4264. if (ret == -EIO) {
  4265. /* Allow ring initialisation to fail by marking the GPU as
  4266. * wedged. But we only want to do this where the GPU is angry,
  4267. * for all other failure, such as an allocation failure, bail.
  4268. */
  4269. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4270. atomic_set_mask(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
  4271. ret = 0;
  4272. }
  4273. out_unlock:
  4274. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4275. mutex_unlock(&dev->struct_mutex);
  4276. return ret;
  4277. }
  4278. void
  4279. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  4280. {
  4281. struct drm_i915_private *dev_priv = dev->dev_private;
  4282. struct intel_engine_cs *ring;
  4283. int i;
  4284. for_each_ring(ring, dev_priv, i)
  4285. dev_priv->gt.cleanup_ring(ring);
  4286. }
  4287. static void
  4288. init_ring_lists(struct intel_engine_cs *ring)
  4289. {
  4290. INIT_LIST_HEAD(&ring->active_list);
  4291. INIT_LIST_HEAD(&ring->request_list);
  4292. }
  4293. void i915_init_vm(struct drm_i915_private *dev_priv,
  4294. struct i915_address_space *vm)
  4295. {
  4296. if (!i915_is_ggtt(vm))
  4297. drm_mm_init(&vm->mm, vm->start, vm->total);
  4298. vm->dev = dev_priv->dev;
  4299. INIT_LIST_HEAD(&vm->active_list);
  4300. INIT_LIST_HEAD(&vm->inactive_list);
  4301. INIT_LIST_HEAD(&vm->global_link);
  4302. list_add_tail(&vm->global_link, &dev_priv->vm_list);
  4303. }
  4304. void
  4305. i915_gem_load(struct drm_device *dev)
  4306. {
  4307. struct drm_i915_private *dev_priv = dev->dev_private;
  4308. int i;
  4309. dev_priv->objects =
  4310. kmem_cache_create("i915_gem_object",
  4311. sizeof(struct drm_i915_gem_object), 0,
  4312. SLAB_HWCACHE_ALIGN,
  4313. NULL);
  4314. dev_priv->vmas =
  4315. kmem_cache_create("i915_gem_vma",
  4316. sizeof(struct i915_vma), 0,
  4317. SLAB_HWCACHE_ALIGN,
  4318. NULL);
  4319. dev_priv->requests =
  4320. kmem_cache_create("i915_gem_request",
  4321. sizeof(struct drm_i915_gem_request), 0,
  4322. SLAB_HWCACHE_ALIGN,
  4323. NULL);
  4324. INIT_LIST_HEAD(&dev_priv->vm_list);
  4325. i915_init_vm(dev_priv, &dev_priv->gtt.base);
  4326. INIT_LIST_HEAD(&dev_priv->context_list);
  4327. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4328. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4329. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4330. for (i = 0; i < I915_NUM_RINGS; i++)
  4331. init_ring_lists(&dev_priv->ring[i]);
  4332. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  4333. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4334. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4335. i915_gem_retire_work_handler);
  4336. INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
  4337. i915_gem_idle_work_handler);
  4338. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4339. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  4340. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  4341. dev_priv->num_fence_regs = 32;
  4342. else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4343. dev_priv->num_fence_regs = 16;
  4344. else
  4345. dev_priv->num_fence_regs = 8;
  4346. if (intel_vgpu_active(dev))
  4347. dev_priv->num_fence_regs =
  4348. I915_READ(vgtif_reg(avail_rs.fence_num));
  4349. /* Initialize fence registers to zero */
  4350. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4351. i915_gem_restore_fences(dev);
  4352. i915_gem_detect_bit_6_swizzle(dev);
  4353. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4354. dev_priv->mm.interruptible = true;
  4355. i915_gem_shrinker_init(dev_priv);
  4356. mutex_init(&dev_priv->fb_tracking.lock);
  4357. }
  4358. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4359. {
  4360. struct drm_i915_file_private *file_priv = file->driver_priv;
  4361. /* Clean up our request list when the client is going away, so that
  4362. * later retire_requests won't dereference our soon-to-be-gone
  4363. * file_priv.
  4364. */
  4365. spin_lock(&file_priv->mm.lock);
  4366. while (!list_empty(&file_priv->mm.request_list)) {
  4367. struct drm_i915_gem_request *request;
  4368. request = list_first_entry(&file_priv->mm.request_list,
  4369. struct drm_i915_gem_request,
  4370. client_list);
  4371. list_del(&request->client_list);
  4372. request->file_priv = NULL;
  4373. }
  4374. spin_unlock(&file_priv->mm.lock);
  4375. if (!list_empty(&file_priv->rps.link)) {
  4376. spin_lock(&to_i915(dev)->rps.client_lock);
  4377. list_del(&file_priv->rps.link);
  4378. spin_unlock(&to_i915(dev)->rps.client_lock);
  4379. }
  4380. }
  4381. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4382. {
  4383. struct drm_i915_file_private *file_priv;
  4384. int ret;
  4385. DRM_DEBUG_DRIVER("\n");
  4386. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4387. if (!file_priv)
  4388. return -ENOMEM;
  4389. file->driver_priv = file_priv;
  4390. file_priv->dev_priv = dev->dev_private;
  4391. file_priv->file = file;
  4392. INIT_LIST_HEAD(&file_priv->rps.link);
  4393. spin_lock_init(&file_priv->mm.lock);
  4394. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4395. ret = i915_gem_context_open(dev, file);
  4396. if (ret)
  4397. kfree(file_priv);
  4398. return ret;
  4399. }
  4400. /**
  4401. * i915_gem_track_fb - update frontbuffer tracking
  4402. * old: current GEM buffer for the frontbuffer slots
  4403. * new: new GEM buffer for the frontbuffer slots
  4404. * frontbuffer_bits: bitmask of frontbuffer slots
  4405. *
  4406. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4407. * from @old and setting them in @new. Both @old and @new can be NULL.
  4408. */
  4409. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4410. struct drm_i915_gem_object *new,
  4411. unsigned frontbuffer_bits)
  4412. {
  4413. if (old) {
  4414. WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
  4415. WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
  4416. old->frontbuffer_bits &= ~frontbuffer_bits;
  4417. }
  4418. if (new) {
  4419. WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
  4420. WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
  4421. new->frontbuffer_bits |= frontbuffer_bits;
  4422. }
  4423. }
  4424. /* All the new VM stuff */
  4425. unsigned long
  4426. i915_gem_obj_offset(struct drm_i915_gem_object *o,
  4427. struct i915_address_space *vm)
  4428. {
  4429. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4430. struct i915_vma *vma;
  4431. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4432. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4433. if (i915_is_ggtt(vma->vm) &&
  4434. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4435. continue;
  4436. if (vma->vm == vm)
  4437. return vma->node.start;
  4438. }
  4439. WARN(1, "%s vma for this object not found.\n",
  4440. i915_is_ggtt(vm) ? "global" : "ppgtt");
  4441. return -1;
  4442. }
  4443. unsigned long
  4444. i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
  4445. const struct i915_ggtt_view *view)
  4446. {
  4447. struct i915_address_space *ggtt = i915_obj_to_ggtt(o);
  4448. struct i915_vma *vma;
  4449. list_for_each_entry(vma, &o->vma_list, vma_link)
  4450. if (vma->vm == ggtt &&
  4451. i915_ggtt_view_equal(&vma->ggtt_view, view))
  4452. return vma->node.start;
  4453. WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
  4454. return -1;
  4455. }
  4456. bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
  4457. struct i915_address_space *vm)
  4458. {
  4459. struct i915_vma *vma;
  4460. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4461. if (i915_is_ggtt(vma->vm) &&
  4462. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4463. continue;
  4464. if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
  4465. return true;
  4466. }
  4467. return false;
  4468. }
  4469. bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
  4470. const struct i915_ggtt_view *view)
  4471. {
  4472. struct i915_address_space *ggtt = i915_obj_to_ggtt(o);
  4473. struct i915_vma *vma;
  4474. list_for_each_entry(vma, &o->vma_list, vma_link)
  4475. if (vma->vm == ggtt &&
  4476. i915_ggtt_view_equal(&vma->ggtt_view, view) &&
  4477. drm_mm_node_allocated(&vma->node))
  4478. return true;
  4479. return false;
  4480. }
  4481. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  4482. {
  4483. struct i915_vma *vma;
  4484. list_for_each_entry(vma, &o->vma_list, vma_link)
  4485. if (drm_mm_node_allocated(&vma->node))
  4486. return true;
  4487. return false;
  4488. }
  4489. unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
  4490. struct i915_address_space *vm)
  4491. {
  4492. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4493. struct i915_vma *vma;
  4494. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4495. BUG_ON(list_empty(&o->vma_list));
  4496. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4497. if (i915_is_ggtt(vma->vm) &&
  4498. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4499. continue;
  4500. if (vma->vm == vm)
  4501. return vma->node.size;
  4502. }
  4503. return 0;
  4504. }
  4505. bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
  4506. {
  4507. struct i915_vma *vma;
  4508. list_for_each_entry(vma, &obj->vma_list, vma_link)
  4509. if (vma->pin_count > 0)
  4510. return true;
  4511. return false;
  4512. }